persistent connections and 'set variable'

From: Marc Fournier <mfournier(at)sd63(dot)bc(dot)ca>
To: "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: persistent connections and 'set variable'
Date: 2013-02-14 23:03:05
Message-ID: 62A9D778-4EDA-43B1-9AF4-B8FF53F608BC@sd63.bc.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc


We have an application that we're trying to get audit logging in place for … right now, we're doing it as a stored procedure that is called by the java app for each time it does an update / insert / delete, but I'd like to move it to a trigger … the problem is (or so we think) is that we're using a persistent connection vs re-establishing a connection …

I don't know the internals of this … if two ppl log into the system (same db role … our auth system is based on a users table), and both were to do a transaction to do an update of a table:

user 1 user 2
begin;
begin;
set variable = 1;
set variable = 2;
update table using variable;
end;
end;

now, since they are both in a transaction, does the connection pool start up one connection per user, or does it somehow 'tag' the queries and re-use the same connection?

What if I did something foolish like avoid the begin/end around the transaction? so user 1 sends his set, then user 2 sends his set, then user 1 tries to update based on the variable? Do the queries get sent across the same connection, and user 2's setting of variable overrides user 1?

I've already tested using psql that if I do:

openstudent=> begin;
BEGIN
openstudent=> set var.modified_by = 1;
SET
openstudent=> end;
COMMIT
openstudent=> show var.modified_by;
var.modified_by
-----------------
1
(1 row)

that variable retains the '1' value … so the end doesn't revert the variable to an unset state …

What I'm trying to do is to get the modified_by id passed down into a trigger, instead of using a stored procedure … current_user doesn't apply, since the login isn't at the 'db role' level … but I need to make sure how it works with the connection pooling, whether what I'm thinking is even possible / reliable …

Thanks ...

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2013-02-15 00:02:57 Re: persistent connections and 'set variable'
Previous Message dmp 2013-02-14 17:16:42 Re: rounded brackets in prepared statement