Spring各种传播特性源码实现的概览
这几天都在分析Spring的源码实现,看到事务管理的部分
我们知道事务的传播特性有下面几种,我标黄的就是最常用的3中传播特性, Sping在发生事务嵌套的时候,会依据内层事务的传播特性,来决定内层是事务的行为
对于传播特性的处理,一定是在外层已经存在事务的情况下进行的, 使用了AOP事务管理 那么在执行外层方法前,进入代理的时候就会调用getTransaction(txAttr)方法来开启事务,此时因为还没有ConnectionHolder创建出来,所以一定会去创建Connection 和 ConnectionHolder 以及开启事务的,这个时候 spring不会关系外层方法对于的事务的传播特性,此时的传播特性也没有什么意义.
到了第二层,这个传播特性才开始起作用了,在外层已经有事务开启的情况下,本层的方法是开启新事务,还是加入上层的事务,还是报错,等等举动,就都取决于这个传播特性了.
- PROPAGATION_MANDATORY 方法必须在事务中运行,如果没有事务就要抛出错误 MANDATORY 是强制的意思
- PROPAGATION_NESTED 嵌套,外层有事务的情况下 如果内层要打开事务就打开事务嵌套运行,如果内层没有事务就加入到上层事务中去,嵌套事务是可以独立提交和回滚的 (对于jdbc Spring其实在内层没有开启新事务,只是在内层方法前设置了savepoint)
- PROPAGATION_NEVER 表示这个方法不能运行在事务中,如果上下文有事务 就要抛出异常
- PROPAGATION_NOT_SUPPORTED 表示当前方法运行的时候 如果有上下文事务,那么上下文事务会被挂起 等该方法执行结束,上下文事务恢复
- PROPAGATION_REQUIRED 表示当前方法必须运行在事务中,如果上下文有事务就加入上下文事务;上下文没有事务,就启动一个新事务
- PROPAGATION_REQUIRES_NEW 当前方法一定会启动自己的事务,如果有上下文事务,上下文事务会被挂起的 (对于jdbc Spring在内层开启新事务(创建了新的Connection 内层事务是独立的开启 提交 回滚的)
- PROPAGATION_SUPPORTS 表示当前方法不需要上下文事务,但是如果有上下文事务的话 还是可以在上下文事务里运行的
/**
* Create a TransactionStatus for an existing transaction.
*/
private TransactionStatus handleExistingTransaction(
TransactionDefinition definition, Object transaction, boolean debugEnabled)
throws TransactionException { //PROPAGATION_NEVER 就是不能再事务环境中运行 如果外层有事务了 就直接抛错
if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {
throw new IllegalTransactionStateException(
"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");
}
//开始挂起外层事务 这会把外层事务的ConnectionHolder ,names isolation 等等属性存到suspendedResources 然后suspendedResources 存到
//本层事务对于的TransactionStatus对象里面
//然后会清除TransactionSynchronizationManager里面的ThreadLocal属性 包括线程绑定的ConnectionHolder ,于是本层dao方法中如果要访问数据库就不能取ThreadLocal里的Connection了
//得自己新建Connection,自然就实现了脱离外层事务的目的
//等到本层dao方法结束,进行commit的时候 会跳过doCommit方法 执行resume方法 恢复外层事务
Object suspendedResources = suspend(transaction); //newSynchronization 肯定是false
boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
return prepareTransactionStatus(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 {
//开启本层新事务
boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);//也是true
//创建txStatus对象 isNewTransaction是true
DefaultTransactionStatus status = newTransactionStatus( definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
//执行conn.setAutoCommit(false) 以及ConnectionHolder状态设置,以及ConnectionHolder线程绑定
doBegin(transaction, definition);
//TransactionSynchronizationManager属性填充
prepareSynchronization(status, definition); return status;
}
catch (RuntimeException beginEx) {
//如果出异常 就恢复上层事务
resumeAfterBeginException(transaction, suspendedResources, beginEx);
throw beginEx;
}
catch (Error beginErr) {
resumeAfterBeginException(transaction, suspendedResources, beginErr);
throw beginErr;
}
} //PROPAGATION_NESTED 是嵌套,但是spring实现jdbc的嵌套并没有开启事务,而是通过设置savepoint来实现的
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() + "]");
}
//如果允许使用savepoint来实现Nest
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.
//不开启新事务 直接创建txStatus
DefaultTransactionStatus status = prepareTransactionStatus(definition, transaction, false, false, debugEnabled, null);
//创建savepoint
status.createAndHoldSavepoint();
return status;
}
else {
//如果不允许使用savepoint 那就和PROPAGATION_REQUIRES_NEW 是一样的 本层开启全新事务 区别在于 这里是没有对上层事务做挂起
//这种事情主要用在JTA事务
// 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;
}
} // 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;
throw new IllegalTransactionStateException("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()) {
throw new IllegalTransactionStateException("Participating transaction with definition [" +
definition + "] is not marked as read-only but existing transaction is");
}
}
} //PROPAGATION_SUPPORTS 和 PROPAGATION_REQUIRED的情况 处理方式是一样的 也就是不做任何处理 直接生产TransactionStatus对象就返回了 本层直接加入上层事务
boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
return prepareTransactionStatus(definition, transaction, false, newSynchronization, debugEnabled, null);
}
Spring各种传播特性源码实现的概览的更多相关文章
- spring事务传播实现源码分析
转载. https://blog.csdn.net/qpfjalzm123/article/details/83717367 本文只是对spring事务传播实现的流程进行简单的分析,如有不对之处请指出 ...
- spring事务概念与获取事务时事务传播行为源码分析
一.事务状态:org.springframework.transaction.TransactionStatus isNewTransaction 是否是新事务 hasSavepoint 是否有保存点 ...
- Spring 事物传播特性
Spring 事物传播特性 这是Spring官方的定义 一共有7种 摘自源码省略了一部分 public interface TransactionDefinition { int PROPAGATIO ...
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- Spring框架之beans源码完全解析
导读:Spring可以说是Java企业开发里最重要的技术.而Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Oriented Programmin ...
- Spring框架之websocket源码完全解析
Spring框架之websocket源码完全解析 Spring框架从4.0版开始支持WebSocket,先简单介绍WebSocket协议(详细介绍参见"WebSocket协议中文版" ...
- Spring框架之事务源码完全解析
Spring框架之事务源码完全解析 事务的定义及特性: 事务是并发控制的单元,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务将逻辑相关的一组操作绑定在一 ...
- Spring框架之jms源码完全解析
Spring框架之jms源码完全解析 我们在前两篇文章中介绍了Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Oriented Programmi ...
- Spring框架之spring-webmvc源码完全解析
Spring框架之spring-webmvc源码完全解析 Spring框架提供了构建Web应用程序的全功能MVC模块.Spring MVC分离了控制器.模型对象.分派器以及处理程序对象的角色,支持多种 ...
随机推荐
- ☀【CSS3】形状
CSS3shapeshttp://www.css3shapes.com/ <!DOCTYPE html> <html lang="zh-CN"> <h ...
- log4net.dll配置以及在项目中应用 zt
1 首先在项目中引用log4net.dll,然后项目中添加一个配置文件log4net.config <?xml version="1.0" encoding="ut ...
- PowerDesigner 怎么给 Table Properties 增加注释和默认值
1. 选中表,右键 2. 选中“comment”, 这个就是列的注释 3.还是这个页面 ,往下有个“default value”, 这个就是你设置的默认值. 4. 这个是怎么设置默认值.
- Windows游戏编程之从零开始d
Windows游戏编程之从零开始d I'm back~~恩,几个月不见,大家还好吗? 这段时间真的好多童鞋在博客里留言说或者发邮件说浅墨你回来继续更新博客吧. woxiangnifrr童鞋说每天都在来 ...
- Unity给力插件之ShaderForge(一)
这是一个用来制作shader的插件,也是一个很好的学习shader的工具.这个插件上手很容易,但是要用它来制作理想的Shader,需要下点功夫. 这儿先列举出基础知识,以及我的一些实践.以后我还会继续 ...
- pow(x,n) leecode
https://oj.leetcode.com/problems/powx-n/ 提交地址 快速幂的使用,可以研究一下 public class Solution { public double po ...
- DevExpress的JavaScript脚本智能提示
http://www.cnblogs.com/zhaozhan/archive/2011/06/08/2075767.html ASPxScriptIntelliSense.js在安装目录下的Comp ...
- (转)PQ分区魔术师中文版分区教程
PQ分区魔术师中文版分区的图解,图文并茂很多朋友提到硬盘分区,觉得不敢轻易去尝试,怕得不偿失,深度xp系统下载在此分享下pq分区的图解详见下图: 1)这是用的雨林木风系统的光盘,其他系统盘一样 2)首 ...
- [Android Framework]linux 文件系统
目录名 bin 用户二进制工具 boot Linux内核镜像文件, 由bootloader程序读取并装载 dev 各种系统硬件设备 etc 系统配置文件及其他配置文件 home 用户工作目录 li ...
- 采集爬虫中,解决网站限制IP的问题? - wendi_0506的专栏 - 博客频道 - CSDN.NET
采集爬虫中,解决网站限制IP的问题? - wendi_0506的专栏 - 博客频道 - CSDN.NET undefined