Re: simultaneous use of JDBC and libpq - Mailing list pgsql-general

From Richard Huxton
Subject Re: simultaneous use of JDBC and libpq
Date
Msg-id 40D9A0CF.508@archonet.com
Whole thread Raw
In response to simultaneous use of JDBC and libpq  (alltest1 <alltest1@covad.net>)
Responses Re: simultaneous use of JDBC and libpq
List pgsql-general
alltest1 wrote:
> Hi,
>
> I am wondering if it is thread-safe to use both JDBC and libpq
> simultaneously.
>
> On a Linux, JDBC is used by Tomcat and libpq is used by a client
> software written in C language. So JDBC and libpq are used by two
> different programs.
> If the same row in the same table is updated (update SQL command)
> through libpq and
> read (select SQL command) by JDBC, then would it cause a thread problem?
> I am not using any transaction, and using just select, update, and
> sometimes insert.

There are no threads involved here - Tomcat/client-app will be different
processes, and both have their own connection to the database.

You might need to consider concurrency issues within the database
though, e.g. assume your bank account has £100 and TOMCAT is debiting
£10 while the other app is crediting £15

TOMCAT: SELECT amount FROM bank_accounts WHERE acct_id = 1;
OTHER:  SELECT amount FROM bank_accounts WHERE acct_id = 1;
TOMCAT: UPDATE bank_accounts SET amount=90  WHERE acct_id = 1;
OTHER:  UPDATE bank_accounts SET amount=115 WHERE acct_id = 1;

Oops - not £105 at all. To solve this, you need to lock the table or the
row(s) in question, e.g.
  SELECT FOR UPDATE ... WHERE acct_id = 1;]#

See the section on "concurrency control" in the manuals for full details.

--
  Richard Huxton
  Archonet Ltd

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: psql
Next
From: Tom Lane
Date:
Subject: Re: FW: problems with installing postgres