Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 3D2201337B49 for ; Tue, 18 Jan 2011 17:55:54 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 45447-07 for ; Tue, 18 Jan 2011 21:55:47 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from web33202.mail.mud.yahoo.com (web33202.mail.mud.yahoo.com [209.191.69.150]) by mail.postgresql.org (Postfix) with SMTP id 3DACA133655C for ; Tue, 18 Jan 2011 17:55:45 -0400 (AST) Received: (qmail 36835 invoked by uid 60001); 18 Jan 2011 21:55:44 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1295387744; bh=cl9zWS+zy29kL6l7+bYkokTOwg+RXl5J7TW1VUv7O2k=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type; b=kzvGJFyZTH8rGALaAsfbxVjtcv0JxInaLQR4hih72DQqK/cEvte8ucoecRb3vzg+WrjxN5WX5a+dNxhX6UI+8xZqdT5FFKREzEHLcCWowAyjfY4fp3Sz2vloBvtRtVQXYhMeN6lQiNbjGtIQFNmFW9BkxqNMb+Y27VrtAHrOZi8= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type; b=puUsSt/dF5vf1psJSKWl9ziHr1OzPSdu3FGxMw7hRPctK+uCbicrSgST8hEUU2ZmEGNGVJSe4WvQzP9ISxy3OsJjey8yAKvyJhmYTgm6ZnGM2BewBMT07K4Qxvgx4qm+SX8/2c1C9eVbwI+SBCMlEMw2IE9+UrloGbJZM3rYNIs=; Message-ID: <728504.34967.qm@web33202.mail.mud.yahoo.com> X-YMail-OSG: OCUd_UoVM1mX_Rmfip8Tm6yuiTHtc.SAoog28_Wyd8KcZNr fHbVxQLlZxGprMm150HPnuyplvxQ31s9ua5dsAEPCvRtUsTAZ5QfytdwfLB9 QI_ahw4k7ekvMJENvRG7PTs40PZuaT.jfmMV1F3_NsvavBCI2Zl7T5lxu8XF dE3fanlHo81aqZyrZsmEpu8pwbyG0gjoG9lbWA6LgO_9bbahsKekZZyunGbq r0Ru1huqpabSdXfhtXkkeCwt3bFn7ijBXZf8t88qXofL3Rb31O1DsErcCJ9F FFVsnrWCdsO7m7Kd6WYrbY8vMU706i4fzqWkEs2abrFmXyHdSK8_2pfnfiQt OXNJgAw-- Received: from [199.84.5.210] by web33202.mail.mud.yahoo.com via HTTP; Tue, 18 Jan 2011 13:55:44 PST X-Mailer: YahooMailClassic/11.4.20 YahooMailWebService/0.8.107.285259 Date: Tue, 18 Jan 2011 13:55:44 -0800 (PST) From: Quartz Subject: Re: jdbc spec violation for autocommit=true & addbatch/executeBatch To: Oliver Jowett Cc: pgsql-jdbc@postgresql.org In-Reply-To: <4D3606CD.7080707@opencloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.908 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RFC_ABUSE_POST=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201101/54 X-Sequence-Number: 21386 --- On Tue, 1/18/11, Oliver Jowett wrote: > From: Oliver Jowett > Subject: Re: [JDBC] jdbc spec violation for autocommit=true & addbatch/executeBatch > To: "Quartz" > Cc: pgsql-jdbc@postgresql.org > Received: Tuesday, January 18, 2011, 4:31 PM > 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 > This is very sad. The batch never mean transaction. It happens to be the supporting fixture for the transaction, but also to avoid roundtrips with connection pools and such overhead when not using transaction as autocommit=true suggest. Is is expected that your said 'sync' at every statement would be less performant than a transaction, but it is still more performant than separated statements/connections, especially with prepared statements. IMHO, you should avoid breaking the spec even if it means some performance loss, which "might"* be recovered another time. The main issue here is to impose a transaction that MAY fail when the calling code isn't designed to handle retries because it didn't need to in the first place. *For attempting to preserve performance, I guess the sync is too aggressive, so there should be an agreement to make a lighter new protocol directive to denote the intent of performing the statement alone rather than in a transaction, although the server can buffer these statement. Some kind of 'enqueue' directive, sort of.