Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 123069FB225 for ; Wed, 7 Mar 2007 11:05:21 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 01649-07 for ; Wed, 7 Mar 2007 11:05:07 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from eppesuigoccas.homedns.org (host245-172-dynamic.0-87-r.retail.telecomitalia.it [87.0.172.245]) by postgresql.org (Postfix) with ESMTP id 93D359FB1F6 for ; Wed, 7 Mar 2007 11:05:07 -0400 (AST) Received: from 89-96-243-184.ip14.fastwebnet.it ([89.96.243.184] helo=scarafaggio) by eppesuigoccas.homedns.org with esmtpsa (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1HOxdY-0005OG-NX for pgsql-jdbc@postgresql.org; Wed, 07 Mar 2007 16:01:06 +0100 Received: from giuseppe by scarafaggio with local (Exim 4.63) (envelope-from ) id 1HOxku-00018O-Jq for pgsql-jdbc@postgresql.org; Wed, 07 Mar 2007 16:08:41 +0100 Subject: Re: Transaction atomicity From: Giuseppe Sacco To: pgsql-jdbc@postgresql.org In-Reply-To: References: <1173268752.20645.29.camel@scarafaggio> Content-Type: text/plain Content-Transfer-Encoding: 7bit Date: Wed, 07 Mar 2007 16:08:39 +0100 Message-Id: <1173280119.20645.39.camel@scarafaggio> Mime-Version: 1.0 X-Mailer: Evolution 2.6.3 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.467 tagged_above=0 required=5 tests=AWL, BAYES_50, FORGED_RCVD_HELO X-Spam-Level: * X-Archive-Number: 200703/71 X-Sequence-Number: 16785 Hi Dave, Il giorno mer, 07/03/2007 alle 07.15 -0500, Dave Cramer ha scritto: > 1) What you are observing is quite normal for postgresql. It use MVCC > so it's quite possible for every connection to see the same value for > max (seNR). > which leads us to 2 Thank you very much. I just read the documentation about MVCC and I understand why my code didn't work. > 2) Don't use max() for this. If you have to use max then you have to > lock the record before doing the insert which will slow everyone > down. Postgresql provides you with sequences for exactly this > purpose. Use nextval('sequence_name') to increment it and currval > ('sequence_name') to get the value that your connection just used. Thanks for this suggestion. I have been evaluating the use of sequences in my application, but I was looking for a more portable code (this code have to run on postgresq 8.1, mysql 5, oracle 9.2 and sql-server 2005). As you may know postgresql nextval syntax isn't as required by the standard; and other vendors use different syntax too. Moreover it would make my code really complex since I would need a new sequence for any connected user. So, before starting using sequences, I will try to loop my original INSERT statement in order to see how long is the average looping. Probably if this is less than 4 iterations, then I will go this way. Again, thank you for your help, Giuseppe