Mybatis事务管理
JdbcTransaction使用JDBC的Connection对象的提交和回滚来管理事务,并通过内部的DataSource对象来获取Connection对象。
public class JdbcTransaction implements Transaction { private static final Log log = LogFactory.getLog(JdbcTransaction.class); protected Connection connection; //当前JDBC连接 protected DataSource dataSource; //数据源 protected TransactionIsolationLevel level; //事务级别 protected boolean autoCommmit; public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) { dataSource = ds; level = desiredLevel; autoCommmit = desiredAutoCommit; } public JdbcTransaction(Connection connection) { this.connection = connection; } /** * 延迟加载Connection对象 */ public Connection getConnection() throws SQLException { if (connection == null) { openConnection(); } return connection; } /** * 提交事务 */ public void commit() throws SQLException { if (connection != null && !connection.getAutoCommit()) { if (log.isDebugEnabled()) { log.debug("Committing JDBC Connection [" + connection + "]"); } connection.commit(); } } /** * 回滚事务 */ public void rollback() throws SQLException { if (connection != null && !connection.getAutoCommit()) { if (log.isDebugEnabled()) { log.debug("Rolling back JDBC Connection [" + connection + "]"); } connection.rollback(); } } /** * 关闭事务 */ public void close() throws SQLException { if (connection != null) { resetAutoCommit(); if (log.isDebugEnabled()) { log.debug("Closing JDBC Connection [" + connection + "]"); } connection.close(); } } protected void setDesiredAutoCommit(boolean desiredAutoCommit) { try { if (connection.getAutoCommit() != desiredAutoCommit) { if (log.isDebugEnabled()) { log.debug("Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]"); } connection.setAutoCommit(desiredAutoCommit); } } catch (SQLException e) { throw new TransactionException("Error configuring AutoCommit. " + "Your driver may not support getAutoCommit() or setAutoCommit(). " + "Requested setting: " + desiredAutoCommit + ". Cause: " + e, e); } } /** * 重置事务为自动提交 */ protected void resetAutoCommit() { try { if (!connection.getAutoCommit()) { if (log.isDebugEnabled()) { log.debug("Resetting autocommit to true on JDBC Connection [" + connection + "]"); } connection.setAutoCommit(true); } } catch (SQLException e) { log.debug("Error resetting autocommit to true " + "before closing the connection. Cause: " + e); } } /** * 从数据源获取Connection对象 */ protected void openConnection() throws SQLException { if (log.isDebugEnabled()) { log.debug("Opening JDBC Connection"); } connection = dataSource.getConnection(); if (level != null) { connection.setTransactionIsolation(level.getLevel()); } setDesiredAutoCommit(autoCommmit); }} 而真正创建事务,是通过工厂类JdbcTransactionFactorypublic class JdbcTransactionFactory implements TransactionFactory { public void setProperties(Properties props) { } public Transaction newTransaction(Connection conn) { return new JdbcTransaction(conn); } public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { return new JdbcTransaction(ds, level, autoCommit); }} ManagedTransaction
ManagedTransaction本身并不做任何事务管理,其忽略了事务提交回滚的动作,而是交由容器去管理事务。public class ManagedTransaction { ... public void commit() throws SQLException { // Does nothing } public void rollback() throws SQLException { // Does nothing } ...} 同样ManagedTransaction的创建,是通过ManagedTransactionFactory工厂类public class ManagedTransactionFactory implements TransactionFactory { private boolean closeConnection = true; //关闭事务时是否关闭连接 public void setProperties(Properties props) { if (props != null) { String closeConnectionProperty = props.getProperty("closeConnection"); if (closeConnectionProperty != null) { closeConnection = Boolean.valueOf(closeConnectionProperty); } } } public Transaction newTransaction(Connection conn) { return new ManagedTransaction(conn, closeConnection); } public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { return new ManagedTransaction(ds, level, closeConnection); }