问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/63990609/changes-to-database-session-context-persists-with-pooled-connection-reuse