Skip site navigation (1) Skip section navigation (2)

Problems ensuring uniqueness?

From: Christopher Smith <x(at)xman(dot)org>
To: pgsql-sql(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org
Subject: Problems ensuring uniqueness?
Date: 2001-06-18 21:10:27
Message-ID: 992898628.6594.1.camel@rivest.xdrive.com (view raw or flat)
Thread:
Lists: pgsql-jdbcpgsql-sql
Hi there. I'm currently using Postgresql 7.0.3, accessing using the JDBC
drivers. I'm enforcing the most severe transaction isolation available
(serializable). I'm currently doing something like the following (this
is pseudo code, with the if being performed in Java):

begin
lock table foo in access exclusive mode
select count(*) from foo where key1 = bar, key2 = baz

if count > 1
    rollback
else
    insert into foo (key1, key2, other) values (bar, baz, stuff)
    commit

Now, table foo has a primary key made up of key1 & key2. Without
concurrent access, the code works great. However, provided I use enough
threads, I inevitably get back errors from the database indicating that
I've tried to insert a record which violates the uniquness of the
primary key.

Is there away to avoid this error by doing my SQL & locking properly, or
should I just catch the exception and deal with it?

--Chris


Responses

pgsql-sql by date

Next:From: Ingo OellersDate: 2001-06-18 21:13:08
Subject: Re: Moving between databases
Previous:From: AllaDate: 2001-06-18 20:53:16
Subject: Re: How to build a TRIGGER in POSTGERSQL

pgsql-jdbc by date

Next:From: Tum KhawcharoenpornDate: 2001-06-18 21:21:18
Subject: JDBC Ant Problem
Previous:From: Tum KhawcharoenpornDate: 2001-06-18 21:08:27
Subject: JDBC Ant Problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group