// org.springframework.transaction.support.AbstractPlatformTransactionManager#handleExistingTransactionprivateTransactionStatushandleExistingTransaction(TransactionDefinition definition,Object transaction,boolean debugEnabled)throwsTransactionException{// PROPAGATION_NEVER 禁用事务传播,抛出异常if(definition.getPropagationBehavior()==TransactionDefinition.PROPAGATION_NEVER){thrownewIllegalTransactionStateException("Existing transaction found for transaction marked with propagation 'never'");}// PROPAGATION_NOT_SUPPORTED 不支持事务传播,挂起之前的事务 if(definition.getPropagationBehavior()==TransactionDefinition.PROPAGATION_NOT_SUPPORTED){if(debugEnabled){
logger.debug("Suspending current transaction");}Object suspendedResources =suspend(transaction);boolean newSynchronization =(getTransactionSynchronization()== SYNCHRONIZATION_ALWAYS);// @param definition TransactionAttribute// @param transaction 支持事务的实体(例如:DataSourceTransactionObject)// @param newTransaction 需要创建一个事务(空或真实事务)// @param newSynchronization 需要事务同步(逻辑见上一行:事务同步策略为总是同步时,为true)// @param ...returnprepareTransactionStatus(
definition,null,false, newSynchronization, debugEnabled, suspendedResources);}// PROPAGATION_REQUIRES_NEW, 也挂起之前的事务,但随后创建并启动一个新事务if(definition.getPropagationBehavior()==TransactionDefinition.PROPAGATION_REQUIRES_NEW){if(debugEnabled){
logger.debug("Suspending current transaction, creating new transaction with name ["+
definition.getName()+"]");}SuspendedResourcesHolder suspendedResources =suspend(transaction);try{// 这里与随笔3的处理一样的returnstartTransaction(definition, transaction, debugEnabled, suspendedResources);}catch(RuntimeException|Error beginEx){resumeAfterBeginException(transaction, suspendedResources, beginEx);throw beginEx;}}// PROPAGATION_NESTED,创建一个事务if(definition.getPropagationBehavior()==TransactionDefinition.PROPAGATION_NESTED){if(!isNestedTransactionAllowed()){thrownewNestedTransactionNotSupportedException("Transaction manager does not allow nested transactions by default - "+"specify 'nestedTransactionAllowed' property with value 'true'");}if(debugEnabled){
logger.debug("Creating nested transaction with name ["+ definition.getName()+"]");}// 默认返回true,JTA返回falseif(useSavepointForNestedTransaction()){// Create savepoint within existing Spring-managed transaction,// through the SavepointManager API implemented by TransactionStatus.// Usually uses JDBC 3.0 savepoints. Never activates Spring synchronization.// 相比startTransaction(),内部少执行了一个doBegin()// 方法入参中的两个false分别是:newTransaction、newSynchronizationDefaultTransactionStatus status =prepareTransactionStatus(definition, transaction,false,false, debugEnabled,null);// 调用ConnectHolder创建保存点(基于jdbc)
status.createAndHoldSavepoint();return status;}else{// Nested transaction through nested begin and commit/rollback calls.// Usually only for JTA: Spring synchronization might get activated here// in case of a pre-existing JTA transaction.returnstartTransaction(definition, transaction, debugEnabled,null);}}// 剩下两种事务传播行为都走默认隔离级别的处理——保持在当前事务下就行了// Assumably PROPAGATION_SUPPORTS or PROPAGATION_REQUIRED.if(debugEnabled){
logger.debug("Participating in existing transaction");}if(isValidateExistingTransaction()){if(definition.getIsolationLevel()!=TransactionDefinition.ISOLATION_DEFAULT){Integer currentIsolationLevel =TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();if(currentIsolationLevel ==null|| currentIsolationLevel != definition.getIsolationLevel()){Constants isoConstants =DefaultTransactionDefinition.constants;thrownewIllegalTransactionStateException("Participating transaction with definition ["+
definition +"] specifies isolation level which is incompatible with existing transaction: "+(currentIsolationLevel !=null?
isoConstants.toCode(currentIsolationLevel,DefaultTransactionDefinition.PREFIX_ISOLATION):"(unknown)"));}}if(!definition.isReadOnly()){if(TransactionSynchronizationManager.isCurrentTransactionReadOnly()){thrownewIllegalTransactionStateException("Participating transaction with definition ["+
definition +"] is not marked as read-only but existing transaction is");}}}boolean newSynchronization =(getTransactionSynchronization()!= SYNCHRONIZATION_NEVER);// 剩下的两种默认的事务传播行为(前面的NESTED其实也是)// 然后从这类一路的return,那需要注意的是,这条路将导致——该事务不会走spring事务管理器的doBegin()来开启事务(spring的事务框架其实只是管理、调度的角色,并不是事务的本身!!!)returnprepareTransactionStatus(definition, transaction,false, newSynchronization, debugEnabled,null);}