Lobster Programming
Lobster Programming
Apr 22, 2024 · Backend Development

Why SqlSession Is Thread‑Unsafe and How SqlSessionTemplate Ensures Safety

This article explains why MyBatis SqlSession is not thread‑safe, outlines strategies such as using per‑thread instances or ThreadLocal to secure it, and details how Spring's SqlSessionTemplate leverages dynamic proxies and ThreadLocal storage to provide thread‑safe database operations.

MyBatisSqlSessionThread Safety
0 likes · 3 min read
Why SqlSession Is Thread‑Unsafe and How SqlSessionTemplate Ensures Safety
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Sep 18, 2023 · Backend Development

Why MyBatis SqlSession Crashes with Multiple Threads and How Spring Fixes It

This article examines the ClassCastException that occurs when 100 threads concurrently query MyBatis using a shared SqlSession, analyzes the root cause in DefaultSqlSession and BaseExecutor, and explains how Spring Boot’s MyBatis integration employs SqlSessionTemplate, ThreadLocal binding, and transaction management to guarantee thread‑safe SqlSession usage.

MyBatisSpring BootSqlSession
0 likes · 21 min read
Why MyBatis SqlSession Crashes with Multiple Threads and How Spring Fixes It
Java Backend Technology
Java Backend Technology
Mar 4, 2018 · Backend Development

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.

Java PersistenceMyBatisSpring Integration
0 likes · 7 min read
Why is DefaultSqlSession Not Thread‑Safe and How Does SqlSessionTemplate Ensure Safety?