Inside MyBatis Transactions: The Real Story of JDBC Commit, Rollback & Close
This article demystifies MyBatis transaction handling by explaining the true JDBC methods—setAutoCommit, commit, rollback—and clarifying common misconceptions about create, begin, close, and suspend, while exploring MyBatis’s Transaction, TransactionFactory, and practical code examples that illustrate how commits, rollbacks, and connection closing behave in various scenarios.
When people think of database transactions they often recall the four ACID properties, isolation levels, and the seven propagation behaviors. The latter are not inherent to transactions but are Spring's strategy for applying transactions across multiple database operations within the same thread.
In reality, only commit and rollback exist as actual JDBC commands; create , begin , close , and suspend are merely semantic abstractions at the business or framework level, not real JDBC operations.
The three genuine JDBC transaction methods you must remember are:
conn.setAutoCommit() conn.commit() conn.rollback() conn.close()simply closes the connection and is no longer a transaction method.
1. MyBatis Transaction Interface
public interface Transaction {
Connection getConnection() throws SQLException;
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
}Do not mistake close() for ending a transaction; it merely returns the connection to the pool.
2. TransactionFactory
Two factories exist: one creates JdbcTransaction (a thin wrapper around JDBC) and the other creates ManagedTransaction (delegates transaction management to an external framework such as Spring). <transactionManager type="JDBC" /> The factory type can be configured in mybatis-config.xml.
3. Using Transactions
Both SqlSession and Executor delegate transaction control to the Transaction implementation. Example usage:
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();
}
}Note that Executor does not automatically commit or roll back; you must call these methods explicitly.
4. Special Scenarios
1) Multiple transactions within a single connection lifecycle
Each commit() ends the current transaction, allowing a new one to start. Only the uncommitted transaction can be rolled back.
2) autoCommit=false without commit, only close
If autoCommit is disabled and commit() is omitted, closing the SqlSession triggers a rollback, discarding any uncommitted changes.
3) autoCommit=false without commit or close
The inserted record may be visible under the READ UNCOMMITTED isolation level before the JVM exits, but it will be rolled back when the session is eventually closed.
MyBatis’s JdbcTransaction behaves almost identically to pure JDBC transactions, with the added support for connection pooling. Any update operation (insert, update, delete) is executed within a transaction, and proper try‑catch‑finally handling is essential.
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.
