mabatis的事务管理的源码追踪,看着老杜追了一次然后自己追了一次
1、主程序
public class MyBatisIntroductionTest {
public static void main(String[] args) throws IOException {
//1、创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//2、创建SqlSessionFactory对象
InputStream is =Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//3、创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4、执行sql语句
int count = sqlSession.insert("insertCar");//返回值是受影响的行数,这里传入的是sql语句块的id
System.out.println("插入了"+count+"条数据");
//5、提交(MyBatis默认采用jdbc事务管理器,不会自动提交,需要手动提交)
sqlSession.commit();
//6、关闭资源(只关闭是不会提交的)
sqlSession.close();
}
}
断点设置:SqlSession sqlSession = sqlSessionFactory.openSession();
2、 可以看到mybatis在默认情况下传入的autocommit参数为false(也就是自动开启事务)
public SqlSession openSession() {
return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(), (TransactionIsolationLevel)null, false);
}
3、在这里面利用事务管理器工厂创建了事务管理器
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
DefaultSqlSession var8;
try {
Environment environment = this.configuration.getEnvironment();
TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
//利用事务管理器工厂创建了一个事务管理器
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
Executor executor = this.configuration.newExecutor(tx, execType);
var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
} catch (Exception var12) {
this.closeTransaction(tx);
throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12);
} finally {
ErrorContext.instance().reset();
}
return var8;
}
4、追进去可以发现创建的是一个jdbc的事务管理器
public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
return new JdbcTransaction(ds, level, autoCommit, this.skipSetAutoCommitOnClose);
}
5、接下来会打开并得到一个connection
public Connection getConnection() throws SQLException {
if (this.connection == null) {
this.openConnection();
}
return this.connection;
}
/**
*这一部分直接追好像追不到,是加了断点后追踪到的
*/
protected void openConnection() throws SQLException {
if (log.isDebugEnabled()) {
log.debug("Opening JDBC Connection");
}
this.connection = this.dataSource.getConnection();
if (this.level != null) {
this.connection.setTransactionIsolation(this.level.getLevel());
}
this.setDesiredAutoCommit(this.autoCommit);
}
/**
*this.connection.setAutoCommit(desiredAutoCommit)在这里设置了事务,默认传入的是false
*/
protected void setDesiredAutoCommit(boolean desiredAutoCommit) {
try {
if (this.connection.getAutoCommit() != desiredAutoCommit) {
if (log.isDebugEnabled()) {
log.debug("Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + this.connection + "]");
}
this.connection.setAutoCommit(desiredAutoCommit);
}
} catch (SQLException var3) {
throw new TransactionException("Error configuring AutoCommit. Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: " + desiredAutoCommit + ". Cause: " + var3, var3);
}
}