面试官:问你一个,Spring事务是如何传播的?( 四 )
这个方法里面主要做了六件事:
- 首先从连接池获取连接并保存到DataSourceTransactionObject对象中 。
- 关闭数据库的自动提交 , 也就是开启事务 。
- 获取数据库的隔离级别 。
- 根据属性设置该事务是否为只读事务 。
- 将该事务标识为活动事务(transactionActive=true) 。
- 将ConnectionHolder对象与当前线程绑定 。
private TransactionStatus handleExistingTransaction(TransactionDefinition definition, Object transaction, boolean debugEnabled)throws TransactionException {//不允许有事务 , 直接异常if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {throw new IllegalTransactionStateException("Existing transaction found for transaction marked with propagation 'never'");}//以非事务方式执行操作 , 如果当前存在事务 , 就把当前事务挂起if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {if (debugEnabled) {logger.debug("Suspending current transaction");}//挂起当前事务Object suspendedResources = suspend(transaction);boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);//修改事务状态信息 , 把事务的一些信息存储到当前线程中 , ThreadLocal中return prepareTransactionStatus(definition, null, false, newSynchronization, debugEnabled, suspendedResources);}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 {boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);DefaultTransactionStatus status = newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);doBegin(transaction, definition);prepareSynchronization(status, definition);return status;}catch (RuntimeException | Error beginEx) {resumeAfterBeginException(transaction, suspendedResources, beginEx);throw beginEx;}}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;}}// 省略boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);return prepareTransactionStatus(definition, transaction, false, newSynchronization, debugEnabled, null); }
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 微软Edge迎来一个新的浮层菜单 用于管理下载进度
- 一个亮点解读 创维小湃P3 Pro必须要买的理由
- 华为认证HCIP-GaussDB-OLTP发布,下一个高级DBA会是你吗
- 又黄一个APP,“品牌收购机”成“没落收割机”,收一黄一
- 教你用Siri来控制电脑:真香
- Google AI建立了一个能够分析烘焙食谱的机器学习模型
- 虾米音乐宣布2月5日关停 人们更关心谁将是下一个
- 虾米音乐一个月后关停 我的听歌记录和个人信息怎么办?
- 华为隐藏一个会议神器,一分钟录入1000字,打字慢的可以看看
- 荣耀手环6简评:这是一个有“偏见”的产品