MyBatis源码解析之Transaction事务模块
一、概述
MyBatis是一款优秀的持久层框架,它支持事务控制,能够帮助开发者方便地管理数据的事务。MyBatis的事务管理模块主要由Transaction接口、TransactionFactory接口、TransactionIsolationLevel枚举和JdbcTransaction、ManagedTransaction两个类实现。
二、Transaction接口
Transaction接口定义了事务的基本方法,包括提交事务、回滚事务和关闭事务。一个SqlSessionFactory实例可以创建一个Transaction实例。
public interface Transaction {
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
Integer getTimeout() throws SQLException;
}
三、TransactionFactory接口
TransactionFactory接口定义了创建Transaction实例的方法。
public interface TransactionFactory {
Transaction newTransaction(Connection conn);
/**
* @since 3.1.0
*/
Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit);
void setProperties(Properties props);
}
四、TransactionIsolationLevel枚举
TransactionIsolationLevel枚举定义了事务的隔离级别,包括NONE、READ_UNCOMMITTED、READ_COMMITTED、REPEATABLE_READ和SERIALIZABLE。
五、JdbcTransaction类
JdbcTransaction继承自BaseTransaction类,是基于JDBC实现的事务管理器。它的创建依赖于TransactionFactory实例。在JdbcTransaction对象创建的过程中,如果autoCommit为false,则将会开启事务。
示例代码:
public class JdbcTransaction extends BaseTransaction {
public JdbcTransaction(Connection connection) {
delegate = new JdbcTransactionImpl(connection);
}
public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
Connection conn;
try {
conn = ds.getConnection();
if (conn.getAutoCommit() != desiredAutoCommit) {
conn.setAutoCommit(desiredAutoCommit);
}
delegate = new JdbcTransactionImpl(conn);
} catch (SQLException e) {
throw new TransactionException("Error initializing JDBC Connection. Cause: " + e, e);
}
}
private static class JdbcTransactionImpl extends BaseTransactionState implements TransactionState {
private Connection connection;
private TransactionIsolationLevel level;
private boolean autoCommmit;
public JdbcTransactionImpl(Connection connection) {
this.connection = connection;
try {
this.level = TransactionIsolationLevel.fromValue(connection.getTransactionIsolation());
this.autoCommmit = connection.getAutoCommit();
} catch (SQLException e) {
throw new TransactionException("Error configuring Transaction. Cause: " + e, e);
}
}
public void commit() throws SQLException {
if (connection != null && !connection.isClosed()) {
if (autoCommmit != connection.getAutoCommit()) {
connection.setAutoCommit(autoCommmit);
}
connection.commit();
}
}
public void rollback() throws SQLException {
if (connection != null && !connection.isClosed()) {
if (autoCommmit != connection.getAutoCommit()) {
connection.setAutoCommit(autoCommmit);
}
connection.rollback();
}
}
public void close() throws SQLException {
connection.close();
}
public Integer getTimeout() throws SQLException {
return null;
}
}
}
六、ManagedTransaction类
ManagedTransaction类也继承自BaseTransaction类,是基于容器环境下的事务管理器,它不会commit和rollback事务,而是依赖容器(如Spring)来完成这些操作。也就是说,它将交给上层容器来管理事务。
示例代码:
public class ManagedTransaction extends BaseTransaction {
private Connection connection;
private TransactionIsolationLevel level;
private boolean closeConnection;
public ManagedTransaction(Connection connection, boolean closeConnection) {
this.connection = connection;
this.closeConnection = closeConnection;
this.delegate = new ManagedTransactionImpl(connection);
}
public ManagedTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
try {
Connection conn = dataSource.getConnection();
if (conn.getAutoCommit() != autoCommit) {
conn.setAutoCommit(autoCommit);
}
this.connection = conn;
this.level = level;
this.closeConnection = true;
this.delegate = new ManagedTransactionImpl(connection);
} catch (SQLException e) {
throw new TransactionException("Error getting a connection. Cause: " + e, e);
}
}
private class ManagedTransactionImpl extends BaseTransactionState implements TransactionState {
public ManagedTransactionImpl(Connection connection) {
// Do nothing
}
public void commit() throws SQLException {
// Do nothing
}
public void rollback() throws SQLException {
// Do nothing
}
public void close() throws SQLException {
if (closeConnection && connection != null) {
connection.close();
}
}
public Integer getTimeout() throws SQLException {
return null;
}
}
}
七、示例说明
示例一:
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
// 获取Transaction实例
Transaction tx = sqlSession.getTransaction();
// 关闭事务
tx.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
示例二:
Transaction transaction = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false);
try {
// 执行一些数据库操作
transaction.commit();
} catch (Exception e) {
transaction.rollback();
} finally {
transaction.close();
}
八、总结
MyBatis的Transaction事务模块是MyBatis框架的核心模块之一,它为MyBatis应用程序提供了事务管理功能。MyBatis的事务管理模块由Transaction接口、TransactionFactory接口、TransactionIsolationLevel枚举和JdbcTransaction、ManagedTransaction两个类实现,开发者可以根据自己的具体需求进行选择。在实际开发中,我们应该根据项目的实际情况选择合适的事务管理方式。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:MyBatis源码解析之Transaction事务模块 - Python技术站