`
adapterofcoms
  • 浏览: 75403 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Mybatis-Spring Interactions

 
阅读更多

Spring side:

DataSourceUtils->getConnection(dataSource)->

TransactionSynchronizationManager.getResource(dataSource)

SpringManagedTransaction->getConnection()->this.connection = DataSourceUtils.getConnection(this.dataSource);

 

SqlSessionFactoryBean:
buildSqlSessionFactory():

this.transactionFactory = new SpringManagedTransactionFactory();--> newTransaction(dataSource,..., ...)

    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);

 
Configuration:
configuration.defaultExecutorType = ExecutorType.SIMPLE;
newExecutor(Transaction transaction, ExecutorType executorType);

DefaultSqlSessionFactory:
openSessionFromDataSource/openSessionFromConnection--return SqlSession-->
[
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
final Executor executor = configuration.newExecutor(tx, execType);
return new DefaultSqlSession(configuration, executor, autoCommit);
]

SqlSessionTemplate:

Thread safe, Spring managed, -->Proxy-->

SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);

holder-->DefaultSqlSession:Executor:SIMPLE/BATCH->1:1

SqlSessionUtils.getSqlSession--> SqlSessionHolder[ResourceHolderSupport].requested++,released--, 

if no holder, TransactionSynchronizationManager.registerSynchronization ->

SqlSessionSynchronization[TransactionSynchronization.beforeCommit--> SqlSession.commit-->executor.commit[...flushStatements...]

TransactionSynchronization.beforeCompletion if referenceCount<=0 TransactionSynchronizationManager.unbindResource(sessionFactory)]

 

BATCH: flushStatements before doSelect

mapper->select/insert/update/delete flushCache 

Mybatis不允许在同一个事务并且是同一线程中并存2种执行模式。同一事务不同线程可以2种执行模式并存。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics