I am running a servlet on Tomcat 5.5 which talks to a PostgreSQL DB
(8.1)
using the Jdbc3 driver.
The database is accessed using a DataSource, and so at the start of
each
servlet doPost call I get a new connection, do a setAutoCommit
( false) and
then get on with the processing. At the end if it works I do a commit
otherwise I do a rollback. Then I setAutoCommit( true) and write to a
log table (a single insert which I always want to do) and close the
connection. The servlet is entirely stateless, it keeps nothing from
one invocation to the next other than the DataSource which it opens
during its init( ) method along with a few configuration parameters.
I turned on postgresql logging and for the first request (the two
requests and
their responses were identical - both failed) the postgresql log
said:-
LOG: statement: BEGIN
LOG: statement: SELECT password, role, proxy, pool, company FROM
users WHERE
id = $1
LOG: statement: INSERT INTO users( created, phone, password, role,
origin,
xml, updated, id) VALUES( $1, $2, $3, $4, $5, $6, $7, $8)
LOG: statement: INSERT INTO users( created, phone, password, role,
origin,
xml, updated, id) VALUES( $1, $2, $3, $4, $5, $6, $7, $8)
ERROR: duplicate key violates unique constraint "users_pkey"
LOG: statement: ROLLBACK
LOG: statement: INSERT INTO logs( userid, ip, request, response,
at, success)
VALUES( $1, $2, $3, $4, $5, $6)
LOG: statement: SELECT 1 FROM ONLY "public"."users" x WHERE "id" =
$1 FOR
UPDATE OF x
CONTEXT: SQL statement "SELECT 1 FROM ONLY "public"."users" x
WHERE "id" = $1
FOR UPDATE OF x"
and for the second one it said:
LOG: statement: SELECT password, role, proxy, pool, company FROM
users WHERE
id = $1
LOG: statement: INSERT INTO users( created, phone, password, role,
origin,
xml, updated, id) VALUES( $1, $2, $3, $4, $5, $6, $7, $8)
LOG: statement: INSERT INTO users( created, phone, password, role,
origin,
xml, updated, id) VALUES( $1, $2, $3, $4, $5, $6, $7, $8)
ERROR: duplicate key violates unique constraint "users_pkey"
LOG: statement: INSERT INTO logs( userid, ip, request, response,
at, success)
VALUES( $1, $2, $3, $4, $5, $6)
which is slightly different.
Now the first thing I notice about this is that the first one has
the BEGIN
I would expect, followed by a select (which I recognise) followed
by the
insert that fails (quite why it logs it twice I do not know, it is
certainly
not issued twice) followed by the rollback (because it failed) and
the insert
into the logs table.
Then comes an odd SELECT statement (the one SELECT 1) which I do
not recognise
and I can not find anywhere in my code.