pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Philip Yarra <philip@utiba.com>
To: pgsql-interfaces@postgresql.org
Cc: Bruce Momjian <pgman@candle.pha.pa.us>
Cc: Alvaro Herrera <alvherre@commandprompt.com>
Cc: Robert Gruszka <rgruszka@power.com.pl>
Subject: Re: multiple threads using one connection
Date: Wed, 1 Mar 2006 11:18:13 +1100
Message-ID: <200603011118.14079.philip@utiba.com> (raw)
In-Reply-To: <200602281817.k1SIH7l10966@candle.pha.pa.us>
References: <200602281817.k1SIH7l10966@candle.pha.pa.us>

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.




view thread (4+ messages)

Message-ID: <200603011118.14079.philip@utiba.com>
Permalink:  ../200603011118.14079.philip@utiba.com/
Also on:    postgresql.org/message-id/200603011118.14079.philip@utiba.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-interfaces@postgresql.org
  Cc: philip@utiba.com, pgman@candle.pha.pa.us, alvherre@commandprompt.com, rgruszka@power.com.pl
  Subject: Re: multiple threads using one connection
  In-Reply-To: <200603011118.14079.philip@utiba.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