Mybatis source code analysis [03.SqlSessionFactory]
/ / construct SqlSession
Public interface SqlSessionFactory {
/ / 8 methods can be used to create SqlSession instances
SqlSession openSession ()
/ / Auto submit
SqlSession openSession (boolean autoCommit)
/ / Connect
SqlSession openSession (Connection connection)
/ / transaction isolation level
SqlSession openSession (TransactionIsolationLevel level)
/ / Type of actuator
SqlSession openSession (ExecutorType execType)
SqlSession openSession (ExecutorType execType, boolean autoCommit)
SqlSession openSession (ExecutorType execType, TransactionIsolationLevel level)
SqlSession openSession (ExecutorType execType, Connection connection)
Configuration getConfiguration ()
}
The implementation class of SqlSessionFactory is DefaultSqlSessionFactory
Public class DefaultSqlSessionFactory implements SqlSessionFactory {private final Configuration configuration; public DefaultSqlSessionFactory (Configuration configuration) {this.configuration = configuration;} / / will eventually call two methods: openSessionFromDataSource,openSessionFromConnection / / the following six methods will call openSessionFromDataSource @ Override public SqlSession openSession () {return openSessionFromDataSource (configuration.getDefaultExecutorType (), null, false);} @ Override public SqlSession openSession (boolean autoCommit) {return openSessionFromDataSource (configuration.getDefaultExecutorType (), null, autoCommit) @ Override public SqlSession openSession (ExecutorType execType) {return openSessionFromDataSource (execType, null, false);} @ Override public SqlSession openSession (TransactionIsolationLevel level) {return openSessionFromDataSource (configuration.getDefaultExecutorType (), level, false);} @ Override public SqlSession openSession (ExecutorType execType, TransactionIsolationLevel level) {return openSessionFromDataSource (execType, level, false);} @ Override public SqlSession openSession (ExecutorType execType, boolean autoCommit) {return openSessionFromDataSource (execType, null, autoCommit) } / / the following two methods will call openSessionFromConnection @ Override public SqlSession openSession (Connection connection) {return openSessionFromConnection (configuration.getDefaultExecutorType (), connection);} @ Override public SqlSession openSession (ExecutorType execType, Connection connection) {return openSessionFromConnection (execType, connection);} @ Override public Configuration getConfiguration () {return configuration;} private SqlSession openSessionFromDataSource (ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {Transaction tx = null; try {final Environment environment = configuration.getEnvironment () Final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment (environment); / / generate a transaction tx = transactionFactory.newTransaction (environment.getDataSource (), level, autoCommit) through the transaction factory; / / generate an executor (the transaction is contained in the executor) final Executor executor = configuration.newExecutor (tx, execType); / / then generate a DefaultSqlSession return new DefaultSqlSession (configuration, executor, autoCommit) } catch (Exception e) {/ / if an error occurs in opening a transaction, close it closeTransaction (tx); / / may have fetched a connection so lets call close () throw ExceptionFactory.wrapException ("Error opening session. Cause: "+ e, e);} finally {/ / finally clear the error context ErrorContext.instance (). Reset ();}} private SqlSession openSessionFromConnection (ExecutorType execType, Connection connection) {try {boolean autoCommit; try {autoCommit = connection.getAutoCommit ();} catch (SQLException e) {/ / Failover to true, as most poor drivers / / or databases won't support transactions autoCommit = true } final Environment environment = configuration.getEnvironment (); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment (environment); final Transaction tx = transactionFactory.newTransaction (connection); final Executor executor = configuration.newExecutor (tx, execType); return new DefaultSqlSession (configuration, executor, autoCommit);} catch (Exception e) {throw ExceptionFactory.wrapException ("Error opening session. Cause: "+ e, e);} finally {ErrorContext.instance (). Reset ();}} private TransactionFactory getTransactionFactoryFromEnvironment (Environment environment) {/ / if no transaction factory is configured, return the managed transaction factory if (environment = = null | | environment.getTransactionFactory () = = null) {return new ManagedTransactionFactory ();} return environment.getTransactionFactory () } private void closeTransaction (Transaction tx) {if (tx! = null) {try {tx.close ();} catch (SQLException ignore) {/ / Intentionally ignore. Prefer previous error. }