Why is DefaultSqlSession Not Thread‑Safe and How Does SqlSessionTemplate Ensure Safety?
This article examines the thread‑unsafe nature of MyBatis's DefaultSqlSession, explains how SqlSessionTemplate and SqlSessionManager use proxy and ThreadLocal mechanisms to provide safe database access in Spring‑integrated environments, and presents related interview questions for deeper understanding.
On Saturday I posted a collection of interview questions titled “2018 Complete Java Interview Guide”. In the comments I added a question about the thread‑safety of SqlSession, and this article explores that issue.
1. Thread‑Unsafety of DefaultSqlSession
In MyBatis, SqlSession is the top‑level interface exposed to callers. Implementations include DefaultSqlSession, SqlSessionManager, and the MyBatis‑Spring provided SqlSessionTemplate. The default implementation is DefaultSqlSession, which is not thread‑safe.
2. How SqlSessionTemplate Uses DefaultSqlSession
When integrating MyBatis with Spring, developers typically inject a singleton SqlSessionTemplate. The source code of SqlSessionTemplate shows that it implements the SqlSession interface, acting as a proxy for DefaultSqlSession while ensuring thread safety.
Because DefaultSqlSession is not thread‑safe, it cannot be used as a singleton. Instead, SqlSessionTemplate delegates calls to a thread‑local SqlSession instance, guaranteeing safe concurrent access.
3. How SqlSessionTemplate Guarantees Thread Safety
SqlSessionTemplate creates a proxy that implements SqlSession. Method calls are intercepted by SqlSessionInterceptor, whose invoke method contains the core logic.
The interceptor uses two helper methods, getSqlSession and closeSqlSession, to obtain and release a thread‑bound SqlSession.
4. What Is SqlSessionManager?
SqlSessionManager is another implementation of SqlSession. It has a private constructor and is instantiated via the static newInstance methods, which are overloaded to provide various configuration options.
Its openSession methods delegate to the underlying SqlSessionFactory to create SqlSession instances. Internally, it uses a ThreadLocal variable ( localSqlSession) to bind a SqlSession to the current thread, avoiding repeated creation and improving performance.
5. Summary
In summary:
DefaultSqlSession does not provide thread‑local handling like SqlSessionManager.
SqlSessionManager uses a ThreadLocal variable to bind a SqlSession to each thread, preventing performance loss from repeated creation.
When developing without Spring, you must create a new SqlSession for each operation because DefaultSqlSession is not thread‑safe.
6. Additional Interview Questions
1. Why does the mybatis‑spring framework use the non‑thread‑safe DefaultSqlSession with dynamic proxies instead of a thread‑safe SqlSessionManager?
2. How does DefaultSqlSession employ the Strategy pattern via its Executor component?
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
