Closed hossi97 closed 10 months ago
Hello @hossi97 ,
I'm not sure how, but the only possibility is that the sqlSession
is shared between multiple threads.
As explained in the doc, SqlSession
is not thread safe.
https://mybatis.org/mybatis-3/getting-started.html#sqlsession
Hello @hossi97 ,
I'm not sure how, but the only possibility is that the
sqlSession
is shared between multiple threads. As explained in the doc,SqlSession
is not thread safe. https://mybatis.org/mybatis-3/getting-started.html#sqlsession
@harawata Thanks for your comment. I already read that but It doesn't seem to be related.
As I examined the code structure, I've found the cause of exception.
BaseExecutor holds the localCache. SimpleExecutor inherits from BaseExecutor, and every SimpleExecutor created through openSession() shares the same localCache value.
In a scenario where multiple threads are working with their respective SqlSession, if a specific thread calls SqlSession.close()
, it internally assigns null to the localCache.
Subsequently, when other threads invoke query method to process query, call the localCache method. As a result, NullPointerException occurs.
@hossi97 ,
I think you are confused.
BaseExecutor.localCache
is an instance field, not a static field.
As I wrote, it is only possible if the instance is accessed from multiple thread. Keep debugging and you will be able to find the cause soon. If you focus on your code instead of MyBatis', you'll be able to find it sooner.
I'm going to close this as it's clearly not a MyBatis bug.
MyBatis version
3.5.13
Database vendor and version
Test case or example project
member.xml
MemberMapper.java
MybatisConnectionFactory.java
MemberDao.java
Steps to reproduce
While load test on the idCheck function, error occurs intermittently when many requests come in at the same time.
Expected result
I debugged the code, and there seems to be a problem where the SqlSession is closed in advance while processing the localCache method.
In my case, this problem doen't occur when doen't use mappers. Only occurs when use mappers. But I'm not sure about this.
The only solution I found is to synchronize the idcheck method using the synchronized keyword.
If do like this, no errors will occur. But I'm not sure that this is the right way.
I thought it was a bug. But if it's not a bug, is there a way to fix it?