Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U6ODv-0008KG-Dk for pgsql-jdbc@arkaria.postgresql.org; Fri, 15 Feb 2013 16:33:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U6ODu-0000Og-OI for pgsql-jdbc@arkaria.postgresql.org; Fri, 15 Feb 2013 16:33:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U68nI-00034c-4y for pgsql-jdbc@postgresql.org; Fri, 15 Feb 2013 00:04:48 +0000 Received: from hub.org ([200.46.208.146]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U68nE-0004eC-Qd for pgsql-jdbc@postgresql.org; Fri, 15 Feb 2013 00:04:47 +0000 Received: from maia.hub.org (unknown [200.46.151.188]) by hub.org (Postfix) with ESMTP id 3095A1AD2FA7 for ; Thu, 14 Feb 2013 20:04:42 -0400 (AST) Received: from hub.org ([200.46.208.146]) by maia.hub.org (mx1.hub.org [200.46.151.188]) (amavisd-maia, port 10024) with ESMTP id 29531-04; Fri, 15 Feb 2013 00:04:34 +0000 (UTC) Received: from [10.5.250.150] (remote.ilcs.sd63.bc.ca [142.31.148.2]) by hub.org (Postfix) with ESMTPA id 902611AD2FA5; Thu, 14 Feb 2013 20:04:33 -0400 (AST) Content-Type: multipart/alternative; boundary="Apple-Mail=_F4B5CBBF-0E96-4137-9B51-C377F4A59D92" Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: persistent connections and 'set variable' From: Marc Fournier In-Reply-To: Date: Thu, 14 Feb 2013 16:04:32 -0800 Cc: List Message-Id: <30EBDB4B-E35B-4144-8BC5-F807F6822D2B@sd63.bc.ca> References: <62A9D778-4EDA-43B1-9AF4-B8FF53F608BC@sd63.bc.ca> To: Dave Cramer X-Mailer: Apple Mail (2.1499) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.9 tagged_above=-5 required=4.9 tests=ALL_TRUSTED=-1, BAYES_00=-1.9, HTML_MESSAGE=0.001, RP_MATCHES_RCVD=-0.001 X-Spam-Level: X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org --Apple-Mail=_F4B5CBBF-0E96-4137-9B51-C377F4A59D92 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252 'k, I think that answers it =85 thx On 2013-02-14, at 16:02 , Dave Cramer 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 >=20 > On 2013-02-14 6:52 PM, "Marc Fournier" wrote: >=20 > On 2013-02-14, at 15:29 , Dave Cramer wrote: >=20 >> If you open a connection for each user you will get 2 separate = connections. The pool doesn't do anything fancy >>=20 >=20 > the way it was explained to me is that we aren't opening a connection, = but are using jBoss to handle that =85 and from the database side of = things, the 'user' is always the same =85 >=20 > so, for instance, on two different browsers, I'm logged into the = system, but there is one database connection: >=20 > 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 >=20 > As long as I don't do anything that 'overlap', it will stay at one = connection to the backend indefinitely =85 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 =85=20 >=20 > but his login id may be 1234, while mine is 4321 =85 I need to get = that 1234 -or- 4321 down to the trigger so that I know who modified a = record =85 >=20 > 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 =85 or, at least, = that is what I *think* would happen =85=20 >=20 > Sorry, I'm not wording this very well =85 and maybe what I'm thinking = is not even possible =85 ? >=20 >=20 >=20 >=20 >> On 2013-02-14 6:20 PM, "Marc Fournier" wrote: >>=20 >> We have an application that we're trying to get audit logging in = place for =85 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 =85 the problem is (or so = we think) is that we're using a persistent connection vs re-establishing = a connection =85 >>=20 >> I don't know the internals of this =85 if two ppl log into the system = (same db role =85 our auth system is based on a users table), and both = were to do a transaction to do an update of a table: >>=20 >>=20 >> user 1 user 2 >> begin; >> begin; >> set variable =3D 1; >> set variable =3D = 2; >> update table using variable; >> end; >> end; >>=20 >> 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? >>=20 >> 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? =20 >>=20 >> I've already tested using psql that if I do: >>=20 >> openstudent=3D> begin; >> BEGIN >> openstudent=3D> set var.modified_by =3D 1; >> SET >> openstudent=3D> end; >> COMMIT >> openstudent=3D> show var.modified_by; >> var.modified_by=20 >> ----------------- >> 1 >> (1 row) >>=20 >> that variable retains the '1' value =85 so the end doesn't revert the = variable to an unset state =85=20 >>=20 >> What I'm trying to do is to get the modified_by id passed down into a = trigger, instead of using a stored procedure =85 current_user doesn't = apply, since the login isn't at the 'db role' level =85 but I need to = make sure how it works with the connection pooling, whether what I'm = thinking is even possible / reliable =85 >>=20 >> Thanks ... >>=20 >> =20 >=20 --Apple-Mail=_F4B5CBBF-0E96-4137-9B51-C377F4A59D92 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=windows-1252 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 =85 = and from the database side of things, the 'user' is always the same = =85

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 =85 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 =85 

but his login id may be 1234, while mine is 4321 =85 = I need to get that 1234 -or- 4321 down to the trigger so that I know who = modified a record =85

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 =85 or, at least, that is = what I *think* would happen =85 

Sorry, I'm not wording this very well =85 and maybe = what I'm thinking is not even possible =85 = ?




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 =85 = 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 =85 the problem is (or so we think) is that = we're using a persistent connection vs re-establishing a connection = =85

I don't know the internals of this =85 if two ppl = log into the system (same db role =85 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 =3D = 1;
                  =                     =                 set variable =3D = 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=3D> = begin;
BEGIN
openstudent=3D> set = var.modified_by =3D 1;
SET
openstudent=3D> = end;
COMMIT
openstudent=3D> show = var.modified_by;
 var.modified_by 
-----------------
 1
(1 row)

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

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

Thanks ...

    =                 =   


= --Apple-Mail=_F4B5CBBF-0E96-4137-9B51-C377F4A59D92--