1: What exactly does openSession do in the MyBatis tool class?
Mybatis Tool Class
1 private static final String RESOURCE = "mybatis-config.xml"; 2 private static SqlSessionFactory sqlSessionFactory = null; 3 private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); 4 5 6 7 8 //关闭sqlsession 9 public static void closeSession(){10 SqlSession session = (SqlSession) threadLocal.get(); // 211 threadLocal.set(null);12 if (session !=null){13 session.close();14 }15 }16 17 public static SqlSession getSessionTwo() {18 //读取配置文件19 try {20 InputStream stream = Resources.getResourceAsStream(RESOURCE);21 sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);22 23 return sqlSessionFactory.openSession(); //返回openSession24 } catch (IOException e) {25 e.printStackTrace();26 }27 28 return null;29 }
First click openSession to find a way to use sqlsessionFactory
1 package org.apache.ibatis.session; 2 3 import java.sql.Connection; 4 5 public interface SqlSessionFactory { 6 SqlSession openSession(); 7 8 SqlSession openSession(boolean var1); 9 10 SqlSession openSession(Connection var1);
Then look at the implementation class of this method DefaultSqlSessionFactory
Here mainly returns a method of his own class openSessionFroDataSource() (data source) and assigns autoCommit to false
Then enter this method
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; }
<br>
可以看到他这个方法主要是初始化一些configure.xml的配置信息和DefaultSqlSession
configure.xml的配置信息和DefaultSqlSession
2: Why does the bottom layer of sqlSession.close() in the MyBatis tool class roll back the transaction?
First enter close() to find its implementation class DefaultsqlSession
The close method in DefaultsqlSession
##In addition, as for why close will roll back the transaction before it is submitted, and it will close the transaction after it is submitted
It mainly depends on the implementation class BaseExecutor of Executorsession.close(); 底层为什么可以回滚事务?????<br>DefaultsqlSession里的close方法<br>
public void close() {try {this.executor.close(this.isCommitOrRollbackRequired(false));this.dirty = false; } finally { ErrorContext.instance().reset(); } }
进入Executor接口 查看他的实现类BaseExecutor 找到close()方法<br>
<br>
The above is the detailed content of MyBatis openSession(), close(), and commit() usage explanation. For more information, please follow other related articles on the PHP Chinese website!