Re: simultaneous use of JDBC and libpq

From: Richard Huxton <dev(at)archonet(dot)com>
To: alltest1 <alltest1(at)covad(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: simultaneous use of JDBC and libpq
Date: 2004-06-23 15:25:03
Message-ID: 40D9A0CF.508@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mark Spruill 2004-06-23 15:49:14 Restore a dump with new line characters
Previous Message Tom Lane 2004-06-23 15:17:07 Re: FW: problems with installing postgres