Understanding MyBatis Transaction Mechanisms and JDBC Transaction Behavior
This article explains the real JDBC transaction methods, clarifies common misconceptions about transaction states in MyBatis, details the MyBatis Transaction and TransactionFactory interfaces, and explores special scenarios such as auto‑commit false, missing commit or close, with concrete code examples.
The article starts by correcting the common misunderstanding that a database transaction has four creation‑related methods (create, begin, close, suspend); in reality only conn.setAutoCommit(), conn.commit() and conn.rollback() exist, while conn.close() merely returns the connection to the pool.
It then introduces the MyBatis Transaction interface, which defines four methods: Connection getConnection(), void commit(), void rollback() and void close(). The close() method in MyBatis does not end a transaction but releases the underlying JDBC connection.
Two concrete implementations are described:
JdbcTransaction : a thin wrapper around a JDBC connection that supports connection‑pooling.
ManagedTransaction : a placeholder that delegates transaction management to an external framework such as Spring.
The TransactionFactory simply creates instances of these two classes; its XML configuration can be set with <transactionManager type="JDBC"/> or similar.
Usage examples show a typical MyBatis workflow: open a SqlSession, obtain a mapper, perform insert / update operations, and explicitly call sqlSession.commit() or sqlSession.rollback() inside a try‑catch‑finally block. The article stresses that the executor’s update method marks the session as dirty, which forces a commit or rollback when the session is closed.
Special scenarios are examined:
With autoCommit=false, each commit() starts a new transaction; missing a commit leads to a rollback during sqlSession.close().
If only close() is called without a prior commit, MyBatis rolls back the transaction before returning the connection to the pool.
If neither commit() nor close() is invoked, the transaction remains open until the JVM exits, and any inserted rows may be visible only under the READ UNCOMMITTED isolation level.
Key source snippets are presented verbatim:
public interface Transaction {
Connection getConnection() throws SQLException;
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
} public void close() throws SQLException {
if (connection != null) {
resetAutoCommit();
connection.close();
}
} private boolean isCommitOrRollbackRequired(boolean force) {
return (!autoCommit && dirty) || force;
} public static void main(String[] args) {
SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
try {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// set fields and insert
mapper.insertStudent(student);
sqlSession.commit();
} catch (Exception e) {
sqlSession.rollback();
} finally {
sqlSession.close();
}
}In conclusion, MyBatis’s JDBC transaction handling is almost identical to raw JDBC, with the added convenience of connection‑pool support, and developers must always manage transactions explicitly to avoid unintended rollbacks.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
