Spring Transaction时序图
2020-04-21 14:55:50 4 举报
Spring transaction简单分析
作者其他创作
大纲/内容
commit(TransactionStatus)
getTransaction()
ReflectiveMethodInvocation
Commit如果是nested,有savepoint的 releaseHeldSavePoint()如果是新的Transaction,做doCommit()如果不是新的Transaction,则不做commit,而交由已存在的Transaction提交Rollback如果是nested,做嵌套事务回滚处理如果是新的Transaction,做doRollback()如果没有新建的Transaction,则由线程中前一个Transaction来处理回滚
invokeWithinTransaction()
TransactionAspectSupport
JdkDynamicAopProxy
TransactionSynchronizationManager.unbindResource(obtainDataSource())
isExistingTransaction()
补充:font color=\"#ff9999\
Connection con = txObject.getConnectionHolder().getConnection()connection.commit()
prepareTransactionInfo()
如果存在Transaction:1.PROPAGATION_NEVER -> throw Exception()2.PROPAGATION_NOT_SUPPORTEDsuspend(transaction)挂起当前事务,对当前事务创建SuspendedResourcesHolder,保存当前事务信息,并使用新的数据库连接,且新的数据库连接不在事务里。3.PROPAGATION_REQUIRES_NEWsuspend(transaction)挂起当前事务,对当前事务创建SuspendedResourcesHolder,保存当前事务信息调用doBegin(),设置数据库连接和事务属性,且font color=\"#ff6666\
suspend(transaction)
TransactionAttributeSource.getTransactionAttribute()
doSuspend(transaction)
DataSourceTransactionManager
doCommit(TransactionStatus)
ConnectionHolder = TransactionSynchronizationManager.getResource(obtainDataSource())
methodIdentification
con.setAutoCommit(false)stmt.executeUpdate(\"SET TRANSACTION READ ONLY\")
如果不存在Transaction:1.PROPAGATION_MANDATORY -> throw Exception()2.font color=\"#cc0000\
interceptor.invoke(this)
doBegin1. 首先查看transaction里面是是不是有Connection,没有的话就拿到DataSource创建一个Connection,放到transaction里的ConnectionHolder里面。2. 设置connection一些属性,比如关闭自动提交,以及事务3. TransactionSynchronizationManager.bindResource()关联DataSource和connectionHolder
doGetTransaction
invocation.proceedWithInvocation()
prepareSynchronization()
invocation.proceed()
TransactionInterceptor
DataSourceTransactionManager=determineTransactionManager(txAttribute)
dosuspend会将原来的transaction相关联的Connection释放,并和transaction的信息放在SuspendedResourceHolder里面。重置线程中相关threadLocal变量TransactionSynchronizationManager.setCurrentTransactionName(null)TransactionSynchronizationManager.setCurrentTransactionReadOnly(false);TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(null);TransactionSynchronizationManager.setActualTransactionActive(false);
入口:EnableTransactionManagement -> TransactionManagementConfigurationSelector -> AutoProxyRegistrar -> 注册BeanDefinition internalAutoProxyCreator(InfrastructureAdvisorAutoProxyCreator)-> ProxyTransactionManagementConfiguration -> TransactionInterceptor
txObject.setConnectionHolder(null)
doBegin()
主要拿到transactionManager以及被切的方法
AbstractPlatformTransactionManager
invokeWithinTransaction
processCommit()
doGetTransaction()
invoke()
newTransactionStatus()
this.advised.getInterceptorsAndDynamicInterceptionAdvice
commitTransactionAfterReturning
TransactionInfo = createTransactionIfNecessary
new DataSourceTransactionObject()
invoke()
这里ConnectionHolder一开始是Null,第一次肯定是新的Transaction
TransactionStatus = tm.getTransaction(txAttr)
handleExistingTransaction
0 条评论
回复 删除
下一页