Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 37F659F9F2C for ; Wed, 15 Aug 2007 10:47:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 66466-05 for ; Wed, 15 Aug 2007 10:46:56 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mir3-fs.mir3.com (mail.mir3.com [65.208.188.100]) by postgresql.org (Postfix) with ESMTP id 869389F9DFC for ; Wed, 15 Aug 2007 10:47:01 -0300 (ADT) Received: MIR3-FS.mir3.com 172.16.1.11 from 172.16.2.68 172.16.2.68 via HTTP with MS-WebStorage 6.0.6249 Received: from archimedes by MIR3-FS.mir3.com; 15 Aug 2007 06:46:57 -0700 Subject: Re: autocommit and stored procedures From: Mark Lewis To: roehm@it.usyd.edu.au Cc: pgsql-jdbc@postgresql.org In-Reply-To: References: Content-Type: text/plain Content-Transfer-Encoding: 7bit Organization: MIR3, Inc. Date: Wed, 15 Aug 2007 06:46:57 -0700 Message-Id: <1187185617.23831.16.camel@archimedes> Mime-Version: 1.0 X-Mailer: Evolution 2.8.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/66 X-Sequence-Number: 17379 On Wed, 2007-08-15 at 21:21 +1000, roehm@it.usyd.edu.au wrote: > Hi, > > Which command granularity does the JDBC driver's autocommit have? > Does it commit after each client-side JDBC statement, or does it commit > each individual SQL statement on the server-side? > In particular, does an JDBC autocommit around the call to a stored > procedure > commit each statement within that stored procedure, or does it commit > the > procedure as a whole? > > Example: > > Stored Procedure > ---------------- > CREATE PROCEDURE Test (n varying character,val REAL) > DECLARE > cid INTEGER; > BEGIN > SELECT custid INTO cid > FROM account > WHERE name=n; > > UPDATE checking > SET balance=balance-val > WHERE custid=cid; > END; > > JDBC Code > --------- > Connection conn; > CallableStatement cstmt; > conn.setAutoCommit(true); > cstmt = conn.prepareCall("{call Test(?,?)}"); > cstmt.setString(1, name); > cstmt.setString(2, value); > cstmt.execute(); > > > Does PostgreSQL execute one commit after the execution of Test(), > i.e. do select and update run in one transaction? > Or will PostgreSQL commit after the select and then again after the > update > inside the Test() procedure? > > Does anyone know what the specified behaviour is for JDBC AutoCommit? > > Many thanks > > Uwe The JDBC driver manages the autocommit flag by not beginning a new transaction at all, since in PostgreSQL all statements execute in their own private transactions unless an explicit transaction is started with the BEGIN statement. Therefore, you would expect each individual statement (as recognized by the PG back-end, not by your Java code) to execute and commit individually. I do not know whether or not JDBC specifies a behavior for this case, but given the intentional vagueness of the spec in several areas, I would be very surprised if the the spec weren't either silent or answered, "Executing multiple SQL commands in a single JDBC command is unsupported and may result in unspecified, driver-specific behavior". -- Mark