X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5D05A52EFC for ; Sat, 13 Aug 2005 08:51:32 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 70689-04 for ; Sat, 13 Aug 2005 11:51:28 +0000 (GMT) Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by svr1.postgresql.org (Postfix) with SMTP id 4930B528DA for ; Sat, 13 Aug 2005 08:51:27 -0300 (ADT) Received: (qmail 25079 invoked from network); 13 Aug 2005 11:51:29 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.2?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 13 Aug 2005 11:51:29 -0000 In-Reply-To: <95C038D7-9E9F-43E7-A565-6833D6C5BCA4@sjtu.edu.cn> References: <05B0404F-A541-4EC9-89C1-64BFE35474EF@fastcrypt.com> <95C038D7-9E9F-43E7-A565-6833D6C5BCA4@sjtu.edu.cn> Mime-Version: 1.0 (Apple Message framework v733) Content-Type: text/plain; charset=UTF-8; delsp=yes; format=flowed Message-Id: <4EE11CA6-4533-44CB-B5FD-B99328A9C014@fastcrypt.com> Cc: List Content-Transfer-Encoding: quoted-printable From: Dave Cramer Subject: Re: problem with stored procedure ,transaction and jdbc Date: Sat, 13 Aug 2005 07:51:25 -0400 To: Jiangyi X-Mailer: Apple Mail (2.733) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.013 tagged_above=0 required=5 tests=[AWL=0.013] X-Spam-Level: X-Archive-Number: 200508/95 X-Sequence-Number: 13664 On 12-Aug-05, at 10:30 PM, Jiangyi wrote: > I call the first strored procedure to insert a master record, and =20 > call the second > stored procedure once or more to insert detailed records. I will =20 > not call the > second stored procedure if the first call failed. But there are =20 > situations the second > call will fail because of the table constraints. So I wish if any =20 > of the call to the second > stored procedure fails, rollback them all. Yes, this is the way postgresql works, if anything fails inside the =20 transaction it will be rolled back > > I noticed that many example of postgresql jdbc stored procedure =20 > only set autocommit > to false before calling but none of the example use commit or =20 > rollback in their code. > > I'am not certain about how to use transaction in jdbc along side =20 > stored procedure. > > try { > conn.setAutoCommit(false); > new_deal =3D conn.prepareCall( > "{? =3D call new_deal(?)}"); > new_deal.setString(2, "data"); > new_deal.execute(); > int dealno =3D new_deal.getInt(1); > > new_deal_detail =3D conn.prepareCall( > "{? =3D call new_deal_detail(?,?)}"); > for (int i =3D 0; i < num; i++) { > new_deal_detail.registerOutParameter(1, =20 > Types.BIT); //jdbc bug? why Types.Boolean cannot be used? What version of the driver are you using? Later ones will support =20 Boolean > new_deal_detail.setString(2, "some data"); > new_deal_detail.setInt(3, dealno); > new_deal_detail.execute(); > if (new_deal_detail.getBoolean(1)) { > licenses.add(lics[i]); > i++; > } else { > logger.warning("prelicense generated may be =20= > duplicated. regenerate it."); > } > } > // conn.commit(); > } catch (SQLException ex) { > logger.warning(ex.toString()); > // try { > // conn.rollback(); > // } catch (SQLException ex1) { > // logger.warning(ex1.toString()); > // } > } finally { > try { > new_deal_detail.close(); > new_deal.close(); > conn.close(); > } catch (SQLException ex3) { > logger.warning(ex3.toString()); > } > } > > Here is my code, can it run as I will? > > Another question, can I use save point in JDBC? When I call =20 > conn.setSavepint(), > the call raise a exception to tell me it is not supported? Later versions of the driver should support save points. > > Regards > Jiang > > =E5=9C=A8 2005-8-13=EF=BC=8C=E4=B8=8A=E5=8D=881:09=EF=BC=8CDave Cramer = =E5=86=99=E9=81=93=EF=BC=9A > > >> Yes you can enclose two stored procedures inside a transaction. >> >> Can you call the second one on it's own without the first one =20 >> without getting an exception ? >> >> Dave >> On 12-Aug-05, at 12:47 PM, Jiangyi wrote: >> >> >> >> >>> Hello everyone, >>> I have a probem with stored procedure ,transaction and JDBC. >>> i am confused with the relation between stored procedure, =20 >>> transcation and jdbc. >>> >>> supposed I have code looks like: >>> >>> set autocommit to false >>> call strored procedure 1 >>> call strored procedure 2 >>> when I call the second stored procedure, exception raised >>> so I close the connection and the callable statement. >>> >>> My question is can I enclose the two stored procedure in a parent =20= >>> transcaton >>> use JDBC ? If not , why? >>> >>> Regards >>> Jiang >>> >>> ---------------------------(end of =20 >>> broadcast)--------------------------- >>> TIP 4: Have you searched our list archives? >>> >>> http://archives.postgresql.org >>> >>> >>> >>> >> >> >> ---------------------------(end of =20 >> broadcast)--------------------------- >> TIP 2: Don't 'kill -9' the postmaster >> >> >> > > >