pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: sathish kumar shanmugavelu <sathishkumar.shanmugavelu@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: concurrency problem
Date: Fri, 16 Jun 2006 07:28:16 +0100
Message-ID: <44924F80.7060500@archonet.com> (raw)
In-Reply-To: <ee0ad3a30606152114g122d0021ja7c603a8f36ae62c@mail.gmail.com>
References: <ee0ad3a30606152114g122d0021ja7c603a8f36ae62c@mail.gmail.com>

sathish kumar shanmugavelu wrote:
>             INSERT INTO rcp_patient_visit_monitor (
>                   entry_no, patient_id, visit_date, is_newpatient,
> visit_type, is_medical,
>                   is_review, is_labtest, is_scan, is_scopy, is_xray,
> weight, height)
>                   VALUES ((SELECT coalesce(max(entry_no)+1, 1) FROM
> rcp_patient_visit_monitor),?,current_timestamp,?,?,?,?,?,?,?,?,?,?)
> 
>    The point to note here is the select statement which gets the max
> entry_no and adds one to it and save the new value. entry_no is the primary
> key of the above table.
> 
>   Now i run the same program (different instance) from two systems, save
> the form simultaneously, only one entry is saved, in the other system the
> error says - duplicate key violates.

BEGIN;
LOCK TABLE ...
INSERT ...
COMMIT;

You'll need to handle possible errors where one client fails to get a 
lock and times out. It won't happen often, but you do need to consider 
the option.

-- 
   Richard Huxton
   Archonet Ltd



view thread (21+ messages)  latest in thread

Message-ID: <44924F80.7060500@archonet.com>
Permalink:  ../44924F80.7060500@archonet.com/
Also on:    postgresql.org/message-id/44924F80.7060500@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, sathishkumar.shanmugavelu@gmail.com
  Subject: Re: concurrency problem
  In-Reply-To: <44924F80.7060500@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox