Re: Row-Level Locking?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Greg Zoller <gzoller(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Row-Level Locking?
Date: 2001-05-22 20:10:30
Message-ID: Pine.BSF.4.21.0105221307150.63661-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-jdbc

On 22 May 2001, Greg Zoller wrote:

> I hope someone can help me with this problem.
>
> I have a Java program that performs business logic operations on several
> tables (reads/writes). These operations are logically one transaction,
> and I protect them inside a transaction in Postgres (auto commit=false).
> If something fails during processing of these operations, everything
> rolls back. This works fine.
>
> My problem is this: what happens if half-way through this set of
> operations someone else (perhaps in another thread) attempts to execute
> the same operation? This could corrupt my data. Other threads have no
> way of knowing that someone else is already working with these tables.
>
> My desired behavior is to be able to lock a single table row and have all
> other connections trying to write (and also read, if possible) this row block
> until the logical operation is complete and the lock released.

You might consider using select for update on the rows you want to modify
before the modification which will grab a row lock and force later
transactions to wait for the lock to be released. You have to be careful
that you don't deadlock yourself if you need to be grabbing locks on
multiple tables.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Maurice Balick 2001-05-22 20:21:04 Re: production java/postgresql
Previous Message Stephan Szabo 2001-05-22 20:06:30 Re: Multiple database - multiple query - multiple server

Browse pgsql-jdbc by date

  From Date Subject
Next Message Bruce Momjian 2001-05-22 21:48:37 Re: [HACKERS] Re: JDBC commit to 7.1.2
Previous Message Bruce Momjian 2001-05-22 19:20:01 Re: JDBC commit to 7.1.2