From: Mark Lewis <mark.lewis@mir3.com>
To: roehm@it.usyd.edu.au
Cc: pgsql-jdbc@postgresql.org
Subject: Re: autocommit and stored procedures
Date: Wed, 15 Aug 2007 06:46:57 -0700
Message-ID: <1187185617.23831.16.camel@archimedes> (raw)
In-Reply-To: <D3A9FBB1-6304-4B25-878D-E0F1C06280BA@it.usyd.edu.au>
References: <D3A9FBB1-6304-4B25-878D-E0F1C06280BA@it.usyd.edu.au>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: mark.lewis@mir3.com, roehm@it.usyd.edu.au
Subject: Re: autocommit and stored procedures
In-Reply-To: <1187185617.23831.16.camel@archimedes>
References: <D3A9FBB1-6304-4B25-878D-E0F1C06280BA@it.usyd.edu.au> <1187185617.23831.16.camel@archimedes>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox