一:MyBatis工具類別 中openSession到底做了什麼?
Mybatis工具類別
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 }
先點開openSession 發現踏實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);
o
#
##然後再看這個方法的實作類別DefaultSqlSessionFactory
這裡主要回傳他自己類別的一個方法openSessionFroDataSource() (資料來源) 並賦值autoCommit 為false然後進入這個方法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
二:MyBatis工具類別中sqlSession.close()底層為什麼回滾事務?
先進入close()找到他的實作類別DefaultsqlSession
session.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>
public void close(boolean forceRollback) { //需要传入一个boolean参数try {try {this.rollback(forceRollback); //为true则rollback } finally {if(this.transaction != null) { this.transaction.close(); } } } catch (SQLException var11) { log.warn("Unexpected exception on closing transaction. Cause: " + var11); } finally {this.transaction = null;this.deferredLoads = null;this.localCache = null;this.localOutputParameterCache = null;this.closed = true; } }
rrree ##
另外至於為什麼沒有提交之前close會回滾事務,提交了之後則是關閉事務
主要在於Executor的實作類BaseExecutor的
然后再看下executor.close()里的参数
session.commit(); 為什麼能提交事務,他的實作和close()的實先基本上差不多,主要是
.executor.close(.isCommitOrRollbackRequired(------> isCommitOrRollbackRequired( ||
參數的真假,真則提交,假便關閉<br> session會話
forceRollback为true 然后进入rollback(forceRollBack)参数为true 看下最终的rollback方法
#
#
##
##
# ###### ###### ###以上是MyBatis openSession(),close(),和commit()用法解釋的詳細內容。更多資訊請關注PHP中文網其他相關文章!