简体   繁体   中英

Changes to database session context persists with pooled connection reuse

In my application, I have a connection pool that I use to obtain connections to Oracle database.

I need to execute statements that call stored procedures that might affect the database session context/variables so that these changes affect only the current use of the connection.

When I close the connection and obtain another connection from the pool, I want it like a new connection/session at which the effect of the procedure doesn't exist. Unfortunately, this doesn't happen.

So I obtain a connection, calls the following procedure:

PROCEDURE set_empno (empno NUMBER) IS
  BEGIN
    DBMS_SESSION.SET_CONTEXT('app1_ctx', 'empno', empno);
  END;

like this:

CALL APP1_CTX_PACKAGE.SET_EMPNO(11)

and then I execute this query, which works as expected (returning the value 11 ):

SELECT "PRICE", "EMPNO" FROM "ORDERS" WHERE empno = SYS_CONTEXT('app1_ctx', 'empno')

until now everything looks fine, I close the connection (so it returns to the pool) and call pool.getConnection to obtain a connection from the pool (I want it like new without any effects). The problem is if I just after obtaining the connection called:

SYS_CONTEXT('app1_ctx', 'empno')

I get the value 11 that came from the call before closing the connection. I was expecting to get an error or null since I didn't set the value using this connection.

Is there any way that I can reset the session or the connection to act as a new one without any changes to the context or security context or anything like this

Note that I don't want only to reset the app1_ctx , I want to eliminate any changes to the session (I don't know what exactly the user would change in his calls)

Note also that I use this application to access different databases: Oracle, MySQL, SQLServer..etc

For Oracle, you will want to call DBMS_SESSION.RESET_PACKAGE . Calling that procedure resets the session state of all packages so the session will seem like a new session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM