pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Marc Fournier <mfournier@sd63.bc.ca>
To: Dave Cramer <pg@fastcrypt.com>
Cc: List <pgsql-jdbc@postgresql.org>
Subject: Re: persistent connections and 'set variable'
Date: Thu, 14 Feb 2013 16:04:32 -0800
Message-ID: <30EBDB4B-E35B-4144-8BC5-F807F6822D2B@sd63.bc.ca> (raw)
In-Reply-To: <CADK3HHLcxnS94vn_F7f+Eg_yVF2MT1=Qo81R822F_76CtO57jA@mail.gmail.com>
References: <62A9D778-4EDA-43B1-9AF4-B8FF53F608BC@sd63.bc.ca>
	<CADK3HH+uhpDcDgGji7k4i_zs79kJdySe7GSCBafOnGiURi99rg@mail.gmail.com>
	<E37348B0-2B0C-43CF-AE36-9BC0F118C942@sd63.bc.ca>
	<CADK3HHLcxnS94vn_F7f+Eg_yVF2MT1=Qo81R822F_76CtO57jA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>


'k, I think that answers it … thx


On 2013-02-14, at 16:02 , Dave Cramer <pg@fastcrypt.com> wrote:

> Generally the way jboss apps work is a request is made to jboss it gets a connection from the pool does work and returns it. The connection does not actually close. Being logged in has nothing to do with the db. Most ppl handle this by passing the user from the session to the db on each db request. You cannot set something on login and expect it to persist from one db request to the next within the same web session
> 
> On 2013-02-14 6:52 PM, "Marc Fournier" <mfournier@sd63.bc.ca> wrote:
> 
> On 2013-02-14, at 15:29 , Dave Cramer <pg@fastcrypt.com> wrote:
> 
>> If you open a connection for each user you will get 2 separate connections. The pool doesn't do anything fancy
>> 
> 
> the way it was explained to me is that we aren't opening a connection, but are  using jBoss to handle that … and from the database side of things, the 'user' is always the same …
> 
> so, for instance, on two different browsers, I'm logged into the system, but there is one database connection:
> 
> root@staging:/ # ps aux | grep postg
> pgsql 49509  0.3  0.3  163912  12944 ??  SsJ  11:36PM  0:00.13 postgres: openstudent openstudent 10.5.254.55(15338)  (postgres)
> pgsql 53734  0.0  0.0   17912   1688 ??  IsJ  31Jan13  0:00.06 postgres: logger process    (postgres)
> pgsql 53737  0.0  0.5  159816  19908 ??  IsJ  31Jan13  0:00.86 postgres: checkpointer process    (postgres)
> pgsql 53738  0.0  0.1  159816   2716 ??  SsJ  31Jan13  0:08.59 postgres: writer process    (postgres)
> pgsql 53739  0.0  0.1  159816   5940 ??  SsJ  31Jan13  0:08.26 postgres: wal writer process    (postgres)
> pgsql 53740  0.0  0.1  159816   3076 ??  SsJ  31Jan13  1:05.22 postgres: autovacuum launcher process    (postgres)
> pgsql 53741  0.0  0.0   17912   1832 ??  SsJ  31Jan13  1:16.90 postgres: stats collector process    (postgres)
> pgsql 53722  0.0  0.3  159816  10492  3  SJ   31Jan13  0:33.30 /usr/local/bin/postgres -D /usr/local/pgsql/data
> root  49540  0.0  0.0    9668   1400  1  R+J  11:37PM  0:00.00 grep postg
> 
> As long as I don't do anything that 'overlap', it will stay at one connection to the backend indefinitely … I just got a colleague to login, so there are three of us connected, and still only the one backend connection from the application -> database … 
> 
> but his login id may be 1234, while mine is 4321 … I need to get that 1234 -or- 4321 down to the trigger so that I know who modified a record …
> 
> but because of the connection pooling that is happening, I don't think I can safely use 'set variable' at the beginning of the updates, as if he does a 'set variable' part way through my updates,  his will override mine and he'll take ownership of subsequent changes … or, at least, that is what I *think* would happen … 
> 
> Sorry, I'm not wording this very well … and maybe what I'm thinking is not even possible … ?
> 
> 
> 
> 
>> On 2013-02-14 6:20 PM, "Marc Fournier" <mfournier@sd63.bc.ca> wrote:
>> 
>> 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 ...
>> 
>>                       
> 

view thread (3+ messages)

Message-ID: <30EBDB4B-E35B-4144-8BC5-F807F6822D2B@sd63.bc.ca>
Permalink:  ../30EBDB4B-E35B-4144-8BC5-F807F6822D2B@sd63.bc.ca/
Also on:    postgresql.org/message-id/30EBDB4B-E35B-4144-8BC5-F807F6822D2B@sd63.bc.ca

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: mfournier@sd63.bc.ca, pg@fastcrypt.com
  Subject: Re: persistent connections and 'set variable'
  In-Reply-To: <30EBDB4B-E35B-4144-8BC5-F807F6822D2B@sd63.bc.ca>
  References: <62A9D778-4EDA-43B1-9AF4-B8FF53F608BC@sd63.bc.ca> <CADK3HH+uhpDcDgGji7k4i_zs79kJdySe7GSCBafOnGiURi99rg@mail.gmail.com> <E37348B0-2B0C-43CF-AE36-9BC0F118C942@sd63.bc.ca> <CADK3HHLcxnS94vn_F7f+Eg_yVF2MT1=Qo81R822F_76CtO57jA@mail.gmail.com> <30EBDB4B-E35B-4144-8BC5-F807F6822D2B@sd63.bc.ca>

* 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