Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mybatis source code analysis [03.SqlSessionFactory]

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

/ / 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. }

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report