SqlSessionFactory接口中聲明瞭一系列opensession方法,用來返回SqlSession對象。session
而DefaultSqlSessionFactory是他的實現類,實現了其中的方法。fetch
以下:spa
public SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); }
其中openSessionFromDataSource方法以下:code
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = configuration.newExecutor(tx, execType, autoCommit); return new DefaultSqlSession(configuration, executor); } 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中執行器類型(有三種,SimpleExecutor,BatchExecutor,ReuseExecutor),第二個參數爲事物管理等級,第三個是是否自動提交事物。xml
在方法中的操做:獲取environment對象(全局xml文件中配置)對象
經過TransactionFactory工廠建立事物對象blog
建立Executor對象,須要傳入事物對象、environment對象,以及autoCommit。接口
最後建立並返回SqlSession的實現類的對象,並將其須要的參數傳入。get