Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 51C902E0088 for ; Wed, 20 Feb 2008 09:37:52 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 36507-01-6 for ; Wed, 20 Feb 2008 09:37:40 -0400 (AST) Received: from fk-out-0910.google.com (fk-out-0910.google.com [209.85.128.185]) by postgresql.org (Postfix) with ESMTP id D176A2E00A7 for ; Wed, 20 Feb 2008 09:36:34 -0400 (AST) Received: by fk-out-0910.google.com with SMTP id b27so2961235fka.3 for ; Wed, 20 Feb 2008 05:36:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=MRV1ljw3BQE9zcJ76I929NtV99yfMtBYqMh0qn56K5Q=; b=V6NpayzM7o9CstGjg9Yz4lK2np2har8IWfEsnYizYPKswezc+RIbsrgWjilHzj7AVujyv7jqsmwbPSU8jeRQFA2KkxuE45/gxyC4B5Da9i5sNw0sA8DVI80sNCxh5mLyw9fByJJjLdRWQb4F6EOqIOantn9Zt0d4qC2Jx9HCvVc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Ilv63sLCFTe8T8AY2p3T5np1w9uxbry11seTW7vdPkJ3h2ojJv6kt7p6tmYRniFyr6JHOo9JXhOkxWaM9/koWyaZCFJP6200NPSF9vsrG1c11rwD0IIH8SkrAw0mI0n7+PeytXet5B3c7KQD5gz+Lyt4p/EZudvxeZudUJ5FZg8= Received: by 10.82.187.2 with SMTP id k2mr16274646buf.19.1203514592745; Wed, 20 Feb 2008 05:36:32 -0800 (PST) Received: by 10.82.180.9 with HTTP; Wed, 20 Feb 2008 05:36:32 -0800 (PST) Message-ID: <8efd35820802200536n22c75f92u97474ab4d3a9cdee@mail.gmail.com> Date: Wed, 20 Feb 2008 08:36:32 -0500 From: "Paul Tomblin" To: pgsql-jdbc@postgresql.org Subject: Re: Retrieving last InsertedID : INSERT... RETURNING safe ? In-Reply-To: <47BC2798.9010300@enterprisedb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <38245.1203500677@motionsponsor.com> <47BC0AF6.2060500@enterprisedb.com> <87prurrh87.fsf@mnc.ch> <55C92F71-0317-4E91-A092-C22A7A2F1FD8@fastcrypt.com> <47BC1AC4.5010809@gmail.com> <9096E930-A528-4AF7-92B9-6EB1A5A52F84@fastcrypt.com> <47BC2798.9010300@enterprisedb.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200802/165 X-Sequence-Number: 18372 On Feb 20, 2008 8:14 AM, Heikki Linnakangas wrote: > > Dave Cramer wrote: > > > > On 20-Feb-08, at 7:19 AM, Paul Tomblin wrote: > > > >> Dave Cramer wrote: > >>>> Well, that other solution is dangerous in case multiple inserts > >>>> to that table are done concurrently; a quite common usage pattern > >>>> with java web applications handling multiple HTTP requests with > >>>> concurrent java threads.. > >>>> > >>> No it is not dangerous. It is the right way to do it. There is > >>> absolutely no danger in using currval in this manner. > >> > >> Unless you have autocommit on. > >> > > I was going to say there are absolutely no situations where this is not > > true, however in your case autocommit or not it doesn't matter. > > You have a single connection for the entire application and asynchronous > > events using that connection. Autocommit or not it will not work with > > currval. > > > > In your case you must use nextval before doing the insert. > > Now you lost me. By asynchronous events, do you mean NOTIFY/LISTEN? What > exactly is the scenario you're talking about? In my case, we're talking about a system that has dozens of Java processes, many of which access the database. Because the system used to have autocommit on, one process could do the "insert nextval" and commit, and then another process could do an "insert nextval" and commit, and then the first process would do the "select currval" and would probably get the wrong value. That's one reason why I find it simpler to do a "select nextval" and then "insert ?" with the value returned instead of messing around with currval. -- For my assured failures and derelictions I ask pardon beforehand of my betters and my equals in my Calling here assembled, praying that in the hour of my temptations, weakness and weariness, the memory of this my Obligation and of the company before whom it was entered into, may return to me to aid, comfort and restrain.