Re: multiple threads using one connection - Mailing list pgsql-interfaces

From Philip Yarra
Subject Re: multiple threads using one connection
Date
Msg-id 200603011118.14079.philip@utiba.com
Whole thread Raw
In response to Re: multiple threads using one connection  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-interfaces
On Wed, 1 Mar 2006 05:17 am, Bruce Momjian wrote:
> Just to clarify, libpq is thread-safe is each thread uses its own
> connection object.

Hope it's okay if I clarify this a little further, Bruce...

Robert, I think you *should* be able to use the same connection from multiple 
threads so long as you do your own work to avoid concurrent use of the 
connection. I'm sorry, I know less than nothing about Ada, but in C terms 
you'd be using a mutex, acquiring it before your thread issues a statement on 
the connection, reading back all data etc., then releasing the mutex. Other 
threads can then acquire the mutex and use the connection. I assume Ada 
provides some similar sort of concurrency control.

On a side note, the preferred way is to use a connection per thread... and 
given the overhead of thread creation, ISTM the cost of a connection wouldn't 
be that high... but I assume you have some good reason for doing it this way. 
I suppose a connection pool might be another way you could handle this 
requirement.

I say it *should* work because I know this works with ECPG, and AFAIK it uses 
libpq calls, so should be similar for your language binding, but please test 
it with a sample app before assuming that I know what I'm talking about. Can 
someone correct me if I have this wrong? It's been a while since I had to 
think about this stuff...

Regards, Philip.

-- 

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

-----------------
Utiba Pty Ltd 
This message has been scanned for viruses and
dangerous content by Utiba mail server and is 
believed to be clean.



pgsql-interfaces by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: Python psycopg transaction isolation level
Next
From: elein
Date:
Subject: plpython