// 核心的方法:获取事务对象@OverridepublicfinalTransactionStatusgetTransaction(@NullableTransactionDefinitiondefinition)throwsTransactionException{// 如果有给定的事务配置,使用默认的事务配置(隔离级别,传播行为等)TransactionDefinitiondef=(definition!=null?definition:TransactionDefinition.withDefaults());// 获取一个事务或创建一个新的事务,但事务中的连接资源是从当前线程的绑定资源中查找Objecttransaction=doGetTransaction();booleandebugEnabled=logger.isDebugEnabled();// 现有事务被发现if(isExistingTransaction(transaction)){// 检查传播行为,根据传播行为创建内部事务// 如:REQUIRES_NEW 就会挂起外部事务,开启新事物返回returnhandleExistingTransaction(def,transaction,debugEnabled);}// 检查新事务的定义设置。if(def.getTimeout()<TransactionDefinition.TIMEOUT_DEFAULT){thrownewInvalidTimeoutException("Invalid transaction timeout",def.getTimeout());}// 没有发现现有事务->检查传播行为,继续执行// PROPAGATION_MANDATORY该方法必须在事务中运行,如果当前事务不存在,则会抛出一个异常if(def.getPropagationBehavior()==TransactionDefinition.PROPAGATION_MANDATORY){thrownewIllegalTransactionStateException("No existing transaction found for transaction marked with propagation 'mandatory'");}// NESTED,REQUIRED ,REQUIRES_NEW 就会开启事务elseif(def.getPropagationBehavior()==TransactionDefinition.PROPAGATION_REQUIRED||def.getPropagationBehavior()==TransactionDefinition.PROPAGATION_REQUIRES_NEW||def.getPropagationBehavior()==TransactionDefinition.PROPAGATION_NESTED){SuspendedResourcesHoldersuspendedResources=suspend(null);if(debugEnabled){logger.debug("Creating new transaction with name ["+def.getName()+"]: "+def);}try{// 直接启动事务returnstartTransaction(def,transaction,debugEnabled,suspendedResources);}catch(RuntimeException|Errorex){resume(null,suspendedResources);throwex;}}else{// 剩下的传播行为// SUPPORTS NOT_SUPPORTED NEVER// 创建“空”事务:没有实际事务(不会将connection的autocommitted设置为false)if(def.getIsolationLevel()!=TransactionDefinition.ISOLATION_DEFAULT&&logger.isWarnEnabled()){logger.warn("Custom isolation level specified but no actual transaction initiated; "+"isolation level will effectively be ignored: "+def);}booleannewSynchronization=(getTransactionSynchronization()==SYNCHRONIZATION_ALWAYS);returnprepareTransactionStatus(def,null,true,newSynchronization,debugEnabled,null);}}
privateTransactionStatushandleExistingTransaction(TransactionDefinitiondefinition,Objecttransaction,booleandebugEnabled)throwsTransactionException{// 如果是never,因为存在外界事务所以直接抛异常if(definition.getPropagationBehavior()==TransactionDefinition.PROPAGATION_NEVER){thrownewIllegalTransactionStateException("Existing transaction found for transaction marked with propagation 'never'");}// NOT_SUPPORTED表示该方法不应该运行在事务中。如果存在当前事务,在该方法运行期间,当前事务将被挂起if(definition.getPropagationBehavior()==TransactionDefinition.PROPAGATION_NOT_SUPPORTED){if(debugEnabled){logger.debug("Suspending current transaction");}// 挂起外部事务ObjectsuspendedResources=suspend(transaction);// 设置同步器booleannewSynchronization=(getTransactionSynchronization()==SYNCHRONIZATION_ALWAYS);// 该方法不会真实的创建事务// 新创建的TransactionStatus会持有已经挂起的连接资源returnprepareTransactionStatus(definition,null,false,newSynchronization,debugEnabled,suspendedResources);}// REQUIRES_NEWif(definition.getPropagationBehavior()==TransactionDefinition.PROPAGATION_REQUIRES_NEW){if(debugEnabled){logger.debug("Suspending current transaction, creating new transaction with name ["+definition.getName()+"]");}SuspendedResourcesHoldersuspendedResources=suspend(transaction);try{// 挂起外部事务,开启新的事务,新的事务中也会持有已经挂起的连接资源returnstartTransaction(definition,transaction,debugEnabled,suspendedResources);}catch(RuntimeException|ErrorbeginEx){resumeAfterBeginException(transaction,suspendedResources,beginEx);throwbeginEx;}}//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()+"]");}// 通过使用savepoint来生成嵌套事务(mysql)if(useSavepointForNestedTransaction()){DefaultTransactionStatusstatus=prepareTransactionStatus(definition,transaction,false,false,debugEnabled,null);// 创建保存点status.createAndHoldSavepoint();returnstatus;}else{returnstartTransaction(definition,transaction,debugEnabled,null);}}// 其他REQUIRED,SUPPORTS,MANDATORY,融入事务,不存在事务同步// 因为外界有事务,此次开一个空事务即可booleannewSynchronization=(getTransactionSynchronization()!=SYNCHRONIZATION_NEVER);returnprepareTransactionStatus(definition,transaction,false,newSynchronization,debugEnabled,null);}