Re: Retrieving last InsertedID : INSERT... RETURNING safe ?

From: Guillaume Cottenceau <gc(at)mnc(dot)ch>
To: "Heikki Linnakangas" <heikki(at)enterprisedb(dot)com>
Cc: <spastor(at)motionsponsor(dot)com>, <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Retrieving last InsertedID : INSERT... RETURNING safe ?
Date: 2008-02-20 11:37:44
Message-ID: 87prurrh87.fsf@mnc.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

"Heikki Linnakangas" <heikki 'at' enterprisedb.com> writes:

> spastor(at)motionsponsor(dot)com wrote:
>> Only one exception is regarding the best method to retrieve the last
>> inserted id (serial). There are many posts on this topic but i
>> couldn t find a definitive response. I am using postgresql version
>> 8.2.6 and jdbc 8.2_p505, i ve been experiencing INSERT statement
>> with RETURNING myID, using an executeQuery it works great, i
>> retrieve the proper value everytime at least from the few tests i
>> have been doin locally.
>> Is it THAT easy or could this method break somehow in a live environement ?
>
> It is that easy, and that is a good way to do it.
>
> Alternatively, if you need to support older PostgreSQL version for
> example, you could use a "SELECT currval('sequence')" query after the
> INSERT.

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..

--
Guillaume Cottenceau

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2008-02-20 11:53:30 Re: Retrieving last InsertedID : INSERT... RETURNING safe ?
Previous Message spastor 2008-02-20 11:25:47 Re: Retrieving last InsertedID : INSERT... RETURNING safe ?