一、調用SqlSessionFactory對象的openSession方法,其實是調用private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)
方法,通過參數就可以知道,分別是執行器的類型,事務隔離級別和設置是否自動提交,因此,我們就可以得知,我們在創建SqlSession的時候可以指定這些屬性。
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
//獲取Environment信息
final Environment environment = configuration.getEnvironment();
//獲取TransactionFactory信息
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
//創建Transaction對象
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//創建執行器對象Executor
final Executor executor = configuration.newExecutor(tx, execType);
//創建DefaultSqlSession對象並返回
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
二、從configuration中獲取environment、dataSource和transactionFactory信息,創建事務對象Transaction。
補充:後續看了一些博客,說是保證executor不爲空,因爲defaultExecutorType有可能爲空。
三、根據配置信息,執行器信息和自動提交信息創建DefaultSqlSession。