Re: Prepared statement already exists - Mailing list pgsql-general

From WireSpot
Subject Re: Prepared statement already exists
Date
Msg-id b2d4b0380812072341i58c05dcficf8926050b21483c@mail.gmail.com
Whole thread Raw
In response to Re: Prepared statement already exists  (Tomasz Ostrowski <tometzky@batory.org.pl>)
Responses Re: Prepared statement already exists
List pgsql-general
On Mon, Dec 8, 2008 at 09:17, Tomasz Ostrowski <tometzky@batory.org.pl> wrote:
> So:
>
> sql_md5 = md5(sql);
> try {
>        PREPARE sql_md5 AS sql;
> } catch (SQLException e) {
>        if (! e.getSQLState().equals("42P05")) {
>                throw e;
>        }
> }
> EXECUTE sql_md5;

Yeah, well, like I said, I have to write it in PHP and apparently
"old" or should I say "classic" extensions like Postgres don't trigger
specific but generic exceptions (ie. the catch block above, while
possible, is useless). There are even cases where PHP doesn't throw
catchable exceptions but an "older" kind; which can be handled
globally with a custom exception handler, but doing this for an entire
application just for the sake of one part of it is more trouble than
it's worth (in this case).

In case anybody is interested, here is the way I went. I created a
singleton class which contains a static hashtable. Every time a new
instance is requested it queries the pg_prepared_statements table and
adds values from the column "name" to the hashtable. This way the main
method can avoid clashes fairly well.

This mechanism is still not perfect. Technically it is still possible
for race conditions to appear. Apparently (in PHP at least) pg_connect
does persistent connections by default. If this is overlooked it is
possible for two web pages to use the same connection and one of them
to define a statement a short while before another, which would cause
a clash. Because while connections may be shared, the code-side
hashtable is not. Solution: either make sure connections are NOT
shared, or implement a way to properly share the hashtable across
pages. Or implement the whole thing in Postgres, transparently.

pgsql-general by date:

Previous
From: Tomasz Ostrowski
Date:
Subject: Re: Prepared statement already exists
Next
From: Richard Huxton
Date:
Subject: Re: is there any error for my postgresql installation?