Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 225231337B49 for ; Tue, 18 Jan 2011 13:33:13 -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 70244-04 for ; Tue, 18 Jan 2011 17:33:05 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from web33201.mail.mud.yahoo.com (web33201.mail.mud.yahoo.com [209.191.69.149]) by mail.postgresql.org (Postfix) with SMTP id 4C070133798E for ; Tue, 18 Jan 2011 13:33:05 -0400 (AST) Received: (qmail 52256 invoked by uid 60001); 18 Jan 2011 17:33:03 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1295371983; bh=qfVf9ChFzGAUos0lwB1HdUVu1hpAg7zCWlQ4vD/Jack=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=zp7M1OczGf1PDS3msP2fj3MiwKRo49z4PvQOsPMTPNBuhtV3T2gWCfDvW9f24kHYN5Cg+8daDAHgeLg8DQq7rOJeFqHeFE+n8KsUl8cdXjojKTVByfbtNfmGW/juskZwnxGnk3rlPj/er5+w/LSAxV6TRsojmBuvlj1gt661gv4= 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:In-Reply-To:MIME-Version:Content-Type; b=5gP8JKTBcbufNR7BMi3nwhMs922RcZjlJnIl9YlJDiYcHSvSYMppV9cGnz/O4vjAoXu4tYmYP6SQVNr/W8bt4WEEQ53iXPTxtmG/0It/PEC/6/rMwDiUfanLj28kF3v5xKbyzXKKq5swZnJOtWw3VClxy8EPnDzNZXD5lyX7ABE=; Message-ID: <837228.50486.qm@web33201.mail.mud.yahoo.com> X-YMail-OSG: W8NxZGgVM1mrJ3hljt3q1n_ocVddHHYKo381fsM6CsG03bk 0bfD1eGuzW4rvWE42JswxayRMyAZe0u5CqS8xBlM0NrrKS0w4.QvyDZlFEQX n6PQ8uvgrM2vY9HDistxNW0u..RigG1e5CRIAl0LyW.qvGAoSSom8xLZxZBA Ic1jTlKruXZheAMAeJ2.eQs9iqt27jqJsz3P_TydylpXG7sXxL0TKLLxQ.nd IxWIjfA0lxdwMGvmTgkmMiouDe2yFuRc37uHOJWCrW_k.LnntCM9l5tqB9G9 96WJgIoRQEQSscmoWNbjIRtFfi8Q2urasIEzq8jSFRNZVYDX0 Received: from [199.84.5.210] by web33201.mail.mud.yahoo.com via HTTP; Tue, 18 Jan 2011 09:33:03 PST X-Mailer: YahooMailClassic/11.4.20 YahooMailWebService/0.8.107.285259 Date: Tue, 18 Jan 2011 09:33:03 -0800 (PST) From: Quartz Subject: Re: jdbc spec violation for autocommit=true & addbatch/executeBatch To: pgsql-jdbc@postgresql.org In-Reply-To: <654041.58993.qm@web33207.mail.mud.yahoo.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.898 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, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201101/49 X-Sequence-Number: 21381 Forgot some details: JDK 1.6 postgres driver postgresql-9.0-801.jdbc4.jar postgres server 9.0.2, on windoze XP 64bit and linux 64 bits. --- On Tue, 1/18/11, Quartz wrote: > From: Quartz > Subject: [JDBC] jdbc spec violation for autocommit=true & addbatch/executeBatch > To: pgsql-jdbc@postgresql.org > Received: Tuesday, January 18, 2011, 12:28 PM > jdbc spec violation for > autocommit=true & addbatch/executeBatch > > We are moving to postgresql. I have unexpected deadlocks on > batches even though I don't use transaction (I use > autocommit=true). > > The test program is simple. On a 20+ row table with primary > key 'id', one thread does 20 updates added with > statement.addBatch() from id 1 to 20. Meanwhile the other > thread does updates from row id 20 down to 1. > > To facilitate the contention, I used a "pg_sleep(3)" call > inside each update. That gives me time to start both program > (i.e. both threads). > > update data set f1=if(exists(select * from pg_sleep(3)), 1, > 0) where id=1 > update data set f2=if(exists(select * from pg_sleep(3)), 1, > 0) where id=2 > and so on. > > (I just made 20 fields to see the f1, f2 .. in the sql > string to distinguish at which statement a connection is at. > Could have been the same field all the time.) > > (Under mysql 5+: > update data set f1=sleep(3) where id=1 > update data set f2=sleep(3) where id=2 > and so on.) > > In a nutshell, each program should run for about 60 seconds > (20x3 seconds). > > The results: > > Mysql myisam (table locking) never fails, but the 2 > programs are interlaced and take nearly 120s as expected. > Mysql innodb without transaction never fails, never > deadlock. > Mysql innodb with transaction (autocommit=false & > commit()) does deadlock as expected. > > 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." > > This is simply not true for postgresql. > For now I cannot tell if this is a bad driver setting up > the connection, of if postgresql server simply cannot be > wrapped by jdbc for this API. > > All I can tell is that this is a show stopper for everyone > doing simple transaction-less batches, as there is no way to > avoid deadlock risks completely, besides an expensive > (cpu-wise and design-wise) sort of intended statements to > produce a predictable row locking order. > > Yet, I ran many threads doing the id 1..20 updates > concurrently, and guess what? The 2nd instance was blocked > for the entire duration of the 1st instance, showing about > 60 seconds of duration for the statement on row id=1. This > is unacceptable concurrency. > > > > > > -- > Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-jdbc >