Unveiling MyBatis Transaction Mechanics: From JDBC to Real‑World Scenarios
This article demystifies MyBatis transaction handling by clarifying the true JDBC transaction methods, explaining Spring's propagation concepts, detailing the MyBatis Transaction and TransactionFactory interfaces, showcasing code examples, and exploring special cases such as auto‑commit nuances and connection‑pool interactions.
When discussing database transactions, people often mistakenly list the four ACID properties, four isolation levels, and seven propagation attributes; the latter actually belong to Spring's transaction strategy, not the transaction itself.
The transaction itself only has three real methods: conn.setAutoCommit(), conn.commit(), and conn.rollback(). Terms like create , begin , close , and suspend are not JDBC commands; close() merely returns the connection to the pool.
Remember the three genuine transaction methods and ignore the myriad of state names.
1. MyBatis Transaction Interface
public interface Transaction {
Connection getConnection() throws SQLException;
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
}In MyBatis, close() closes the underlying connection, not the transaction.
Transaction hierarchy diagram:
JdbcTransaction : a minimal wrapper around JDBC transaction that adds connection‑pool support.
ManagedTransaction : a placeholder that delegates transaction management to an external framework such as Spring.
2. TransactionFactory
The factory simply creates either a JdbcTransaction or a ManagedTransaction instance.
<transactionmanager type="JDBC" />3. Using Transaction
Both SqlSession and Executor forward commit, rollback, and close operations to the Transaction implementation.
public static void main(String[] args) {
SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
try {
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student();
student.setName("yy");
student.setEmail("[email protected]");
student.setDob(new Date());
student.setPhone(new PhoneNumber("123-2568-8947"));
studentMapper.insertStudent(student);
sqlSession.commit();
} catch (Exception e) {
sqlSession.rollback();
} finally {
sqlSession.close();
}
}Key observations:
A single connection can host multiple transactions; each commit() ends the current transaction.
With autoCommit=false, if you close the session without an explicit commit(), MyBatis automatically rolls back before returning the connection to the pool.
If you neither commit nor close, the transaction remains pending; with an isolation level of READ UNCOMMITTED, the inserted row is visible until the JVM exits, after which MyBatis rolls back.
Conclusion: MyBatis's JdbcTransaction behaves almost identically to a plain JDBC transaction, the only difference being its support for connection‑pool‑managed connections; any INSERT, UPDATE, or DELETE is always executed within a transaction.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
