Re: [POSTGRESQL] LOCKING A ROW

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jesus Contreras" <jcontreras(at)isoco(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, "pgomez(at)isoco(dot)Com" <pgomez(at)isoco(dot)com>, Ozelin López <ozelin(at)isoco(dot)com>
Subject: Re: [POSTGRESQL] LOCKING A ROW
Date: 2002-05-06 15:32:18
Message-ID: 5731.1020699138@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Jesus Contreras" <jcontreras(at)isoco(dot)com> writes:
> ResultSet rs = db.doSelect("SELECT * FROM index FOR UPDATE");
> int id=0;
> if (rs.next()) {
> id = rs.getInt("sec_id");
> id++;
> db.doUpdate("UPDATE index SET sec_id = " + id);
> db.doUpdate("INSERT INTO secuence VALUES (" + id + ",
> 'numero " + id + "')");
> }

This should work as long as you execute all three SQL commands in a
single transaction; otherwise the FOR UPDATE lock isn't being held long
enough to do what you need. I'm not sure of the behavior of doSelect
and doUpdate, but I wonder whether they aren't issuing each command as a
separate transaction. I'd expect to see a "begin" kind of operation at
the top of this fragment, and a "commit" kind of operation at the bottom...

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-05-06 15:48:21 Re: pgaccess
Previous Message Joel Burton 2002-05-06 14:56:32 Re: HOWTO - Random character generation for primary key