面试官:问你一个,Spring事务是如何传播的?( 五 )
这里面也是对每个传播属性的判断 , 先看PROPAGATION_REQUIRES_NEW的处理 , 因为该属性要求每次调用都开启一个新的事务 , 所以首先会将当前事务挂起 , 怎么挂起呢?
protected final SuspendedResourcesHolder suspend(@Nullable Object transaction) throws TransactionException {if (TransactionSynchronizationManager.isSynchronizationActive()) {List suspendedSynchronizations = doSuspendSynchronization();try {Object suspendedResources = null;//第一次进来 , 肯定为null的if (transaction != null) {//吧connectionHolder设置为空suspendedResources = doSuspend(transaction);}//做数据还原操作String name = TransactionSynchronizationManager.getCurrentTransactionName();TransactionSynchronizationManager.setCurrentTransactionName(null);boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();TransactionSynchronizationManager.setCurrentTransactionReadOnly(false);Integer isolationLevel = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(null);boolean wasActive = TransactionSynchronizationManager.isActualTransactionActive();TransactionSynchronizationManager.setActualTransactionActive(false);return new SuspendedResourcesHolder(suspendedResources, suspendedSynchronizations, name, readOnly, isolationLevel, wasActive);}catch (RuntimeException | Error ex) {// doSuspend failed - original transaction is still active...doResumeSynchronization(suspendedSynchronizations);throw ex;}}else if (transaction != null) {// Transaction active but no synchronization active.Object suspendedResources = doSuspend(transaction);return new SuspendedResourcesHolder(suspendedResources);}else {// Neither transaction nor synchronization active.return null;} } protected Object doSuspend(Object transaction) {DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;txObject.setConnectionHolder(null);//解除绑定关系 ,return TransactionSynchronizationManager.unbindResource(obtainDataSource()); }
这里明显是进入第一个if并且会调用到doSuspend方法 , 整体来说挂起事务很简单:首先将DataSourceTransactionObject的ConnectionHolder设置为空并解除与当前线程的绑定 , 之后将解除绑定的ConnectionHolder和其它属性(事务名称、隔离级别、只读属性)通通封装到SuspendedResourcesHolder对象 , 并将当前事务的活动状态设置为false 。 挂起事务之后又通过newTransactionStatus创建了一个新的事务状态并调用doBegin开启事务 , 这里不再重复分析 。 接着来看PROPAGATION_NESTED:
if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {if (!isNestedTransactionAllowed()) {throw new NestedTransactionNotSupportedException("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() + "]");}//默认是可以嵌套事务的if (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.DefaultTransactionStatus status =prepareTransactionStatus(definition, transaction, false, false, debugEnabled, null);//创建回滚点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.boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);DefaultTransactionStatus status = newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);doBegin(transaction, definition);prepareSynchronization(status, definition);return status;}}
推荐阅读
- 微软Edge迎来一个新的浮层菜单 用于管理下载进度
- 一个亮点解读 创维小湃P3 Pro必须要买的理由
- 华为认证HCIP-GaussDB-OLTP发布,下一个高级DBA会是你吗
- 又黄一个APP,“品牌收购机”成“没落收割机”,收一黄一
- 教你用Siri来控制电脑:真香
- Google AI建立了一个能够分析烘焙食谱的机器学习模型
- 虾米音乐宣布2月5日关停 人们更关心谁将是下一个
- 虾米音乐一个月后关停 我的听歌记录和个人信息怎么办?
- 华为隐藏一个会议神器,一分钟录入1000字,打字慢的可以看看
- 荣耀手环6简评:这是一个有“偏见”的产品