mybatis之SqlSessionFactory
java
以上是以前的分析,在mybatis源碼分析之事務管理器裏分析到了事務管理器安全
SqlSession session = sqlSessionFactory.openSession(); //DefaultSqlSessionFactory裏的openSession public SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); } private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { //根據配置獲取環境 final Environment environment = configuration.getEnvironment(); //構建事務工廠 final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); //經過事務工廠建立事務Transaction對象 tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //建立執行器Executor對象 final Executor executor = configuration.newExecutor(tx, execType); //根據configuration,executor建立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(); } }
以前已經分析過事務管理器,下面分析執行器Executorsession
public enum ExecutorType { SIMPLE, REUSE, BATCH }
執行器類型只有三種mybatis
SIMPLE:普通的執行器;源碼分析
REUSE:執行器會重用預處理語句(prepared statements);fetch
BATCH:執行器將重用語句並執行批量更新。this
configuration.newExecutor(tx, execType); //默認執行器類型 protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE; //二級緩存的全局開關,默認開啓緩存 protected boolean cacheEnabled = true; public Executor newExecutor(Transaction transaction, ExecutorType executorType) { //executorType爲null時executorType=ExecutorType.SIMPLE executorType = executorType == null ? defaultExecutorType : executorType; executorType = executorType == null ? ExecutorType.SIMPLE : executorType; Executor executor; //根據執行器類型建立執行器 if (ExecutorType.BATCH == executorType) { executor = new BatchExecutor(this, transaction); } else if (ExecutorType.REUSE == executorType) { executor = new ReuseExecutor(this, transaction); } else { executor = new SimpleExecutor(this, transaction); } //當cacheEnabled爲true時建立CachingExecutor對象 if (cacheEnabled) { executor = new CachingExecutor(executor); } executor = (Executor) interceptorChain.pluginAll(executor); return executor; }
二級緩存開關配置示例spa
<settings> <setting name="cacheEnabled" value="true"/> </settings>
執行器建立後
new DefaultSqlSession(configuration, executor, autoCommit); //DefaultSqlSession構造方法 public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { this.configuration = configuration; this.executor = executor; this.dirty = false; this.autoCommit = autoCommit; }
至此SqlSession建立完成,從以前的幾篇和這篇能清晰的看到從讀取配置文件到SqlSession建立的整個過程.須要注意的是SqlSession 的實例不是線程安全的,是不能被共享的,因此它的最佳的範圍是請求或方法範圍.每一個線程都應該有本身的 SqlSession 實例.