pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Quartz <quartz12h@yahoo.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: jdbc spec violation for autocommit=true & addbatch/executeBatch
Date: Wed, 19 Jan 2011 10:31:57 +1300
Message-ID: <4D3606CD.7080707@opencloud.com> (raw)
In-Reply-To: <654041.58993.qm@web33207.mail.mud.yahoo.com>
References: <654041.58993.qm@web33207.mail.mud.yahoo.com>

Quartz wrote:

> Postgres with transaction (autocommit=false & commit()) does deadlock as expected.
> Postgres without transaction (autocommit=true) deadlocks UNEXPECTEDLY.
> 
> The jdbc javadoc says clearly:
> http://download.oracle.com/javase/6/docs/api/java/sql/Connection.html#setAutoCommit%28boolean%29
> 
> "If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions."

It's worth noting that the javadoc changes without warning between 
releases - there have been many "clarifications" over the years that are 
actually unannounced behavioral changes - have you checked historical 
javadoc to see what it says? Maybe we implemented against a spec that 
said something different.

That said - yes, all statements within addBatch() are run in a single 
transaction. (Autocommit is triggered by the final Sync message, which 
is only sent at the end of the batch)

A related gotcha is that if you execute a multiple statement query (i.e. 
execute("select 1; select 2; select 3") then, again, there is only one 
enclosing autocommit transaction. (This was deliberate, as it mirrored 
the behavior of protocol-version-2 drivers back in the 7.x days)

I'm not sure if we can implement this according to the current spec 
without losing any performance benefit of addBatch()/executeBatch() - 
we'd have to send a Sync after every individual query. As a workaround I 
suggest you just run the queries individually, not in a batch - there 
won't be much performance difference. (And if you really don't care 
about ordering or atomicity as you imply, you could parallelize it 
across multiple connections if latency is a problem)

Oliver



view thread (10+ messages)  latest in thread

Message-ID: <4D3606CD.7080707@opencloud.com>
Permalink:  ../4D3606CD.7080707@opencloud.com/
Also on:    postgresql.org/message-id/4D3606CD.7080707@opencloud.com

 · 

reply

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: oliver@opencloud.com, quartz12h@yahoo.com
  Subject: Re: jdbc spec violation for autocommit=true & addbatch/executeBatch
  In-Reply-To: <4D3606CD.7080707@opencloud.com>
  References: <654041.58993.qm@web33207.mail.mud.yahoo.com> <4D3606CD.7080707@opencloud.com>

* 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