Search code examples
springmybatisoracle12cspring-mybatis

MyBatis+Spring+Jersey with Oracle Seems to reuses SQL sessions


I'm creating a rest service using MyBatis 3.3.1, Spring 4.3, Jersey 2.22 and Oracle 12c. My transactions are being managed by Spring using the DataSourceTransactionManager and the @Transaction annotaion. I am also using a MyBatis pooled data source as my javax.sql.DataSource. What I don't understand if why database sessions are being reused.

In my application, I am setting an oracle client identifier: DBMS_SESSION.SET_IDENTIFIER("my id"). With the debug logging statements, I can see MyBatis creating new sessions for each of the MyBatis sql operations. I also have debug to print out the database session identifier from DBMS_SESSION.UNIQUE_SESSION_ID.

What I don't understand is that if I access my rest endpoint multiple times, the unique session id is the same and the identifier from my last access is still set.

Shouldn't a new oracle session be used every time MyBatis gets a new SQLSession? Why is the oracle session always the same?

Thanks.


Solution

  • Session in Oracle is bound to a connection. You are using connection pooling so after one rest request is finished the connection is returned to the connection pool. Session is not terminated in this case.

    You probably want to clear identifier on returning connection to pool and setting it on retrieving connection from the pool. The exact way to do that depends on the connection pool you are using. For built-in mybatis connection pool see this answer.