Changes to database session context persists with pooled connection reuse

流过昼夜 提交于 2021-02-17 02:47:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!