原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11530611.html

REQUIRED behavior

Spring REQUIRED behavior means that the same transaction will be used if there is an already opened transaction in the current bean method execution context. If there is no existing transaction the Spring container will create a new one. If multiple methods configured as REQUIRED behavior are called in a nested way they will be assigned distinct logical transactions but they will all share the same physical transaction. In short this means that if an inner method causes a transaction to rollback, the outer method will fail to commit and will also rollback the transaction. Let's see an example:

Outer bean

@Autowired
private TestDAO testDAO; @Autowired
private InnerBean innerBean; @Override
@Transactional(propagation=Propagation.REQUIRED)
public void testRequired(User user) {
testDAO.insertUser(user);
try{
innerBean.testRequired();
} catch(RuntimeException e){
// handle exception
}
}

Inner bean

@Override
@Transactional(propagation=Propagation.REQUIRED)
public void testRequired() {
throw new RuntimeException("Rollback this transaction!");
}

Note that the inner method throws a RuntimeException and is annotated with REQUIRED behavior. This means that it will use the same transaction as the outer bean, so the outer transaction will fail to commit and will also rollback.

REQUIRES_NEW behavior

REQUIRES_NEW behavior means that a new physical transaction will always be created by the container. In other words the inner transaction may commit or rollback independently of the outer transaction, i.e. the outer transaction will not be affected by the inner transaction result: they will run in distinct physical transactions.

Outer bean

@Autowired
private TestDAO testDAO; @Autowired
private InnerBean innerBean; @Override
@Transactional(propagation=Propagation.REQUIRED)
public void testRequiresNew(User user) {
testDAO.insertUser(user);
try{
innerBean.testRequiresNew();
} catch(RuntimeException e){
// handle exception
}
}

Inner bean

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void testRequiresNew() {
throw new RuntimeException("Rollback this transaction!");
}

The inner method is annotated with REQUIRES_NEW and throws a RuntimeException so it will set its transaction to rollback but will not affect the outer transaction. The outer transaction is paused when the inner transaction starts and then resumes after the inner transaction is concluded. They run independently of each other so the outer transaction may commit successfully.

Reference

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html

http://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial

Spring Transaction Propagation的更多相关文章

  1. spring Transaction Propagation 事务传播

    spring Transaction中有一个很重要的属性:Propagation.主要用来配置当前需要执行的方法,与当前是否有transaction之间的关系. 我晓得有点儿抽象,这也是为什么我想要写 ...

  2. Spring Transaction属性之Propagation

    spring Transaction中有一个很重要的属性:Propagation.主要用来配置当前需要执行的方法,与当前是否有transaction之间的关系. 我晓得有点儿抽象,这也是为什么我想要写 ...

  3. spring Transaction Management --官方

    原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12.  ...

  4. springboot成神之——spring boot,spring jdbc和spring transaction的使用

    本文介绍spring boot,spring jdbc和spring transaction的使用 项目结构 依赖 application model层 mapper层 dao层 exception层 ...

  5. spring transaction 初识

    spring 事务初识 1.spring事务的主要接口,首先盗图一张,展示出spring 事务的相关接口.Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibern ...

  6. Spring Transaction 使用入门

    一.开篇陈述 1.1 写文缘由 最近在系统学习spring框架IoC.AOP.Transaction相关的知识点,准备写三篇随笔记录学习过程中的感悟.这是第一篇,记录spring Transactio ...

  7. 疑惑的 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L

    在MAVEN项目里面,在整合spring和mybatis在执行数据库操作的时候报出了: java.lang.AbstractMethodError: org.mybatis.spring.transa ...

  8. Spring AOP propagation七种属性值

    <!-- 配置事务通知 --> <tx:advice id="advice" transaction-manager="transactionManag ...

  9. java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer; at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.jav

    在整合spring和mybatis在执行数据库操作的时候报出了: java.lang.AbstractMethodError: org.mybatis.spring.transaction.Sprin ...

随机推荐

  1. [CSP-S模拟测试]:sum(数学+莫队)

    题目传送门(内部题63) 输入格式 第一行有一个整数$id$,表示测试点编号.第一行有一个整数$q$,表示询问组数.然后有$q$行,每行有两个整数$n_i,m_i$. 输出格式 一共有$q$行,每行一 ...

  2. IBatis.Net 下使用SqlBulkCopy 大批量导入数据 问题解决

    SQLBulkCopy是继承SQLClient空间下的一个特殊类,它可以帮助我们以映射的方式把DataTable和DataReader数据大批量导入到数据库对应表中 public void Inert ...

  3. 关于TCP\IP协议的简单理解

    一: HTTP是基于TCP协议的,TCP属于传输层.HTTP属于应用层. 另一个属于传输层的是UDP协议. 但HTTP是基于TCP这个协议,不是UDP这个协议. TCP相比于UDP,更安全,稳定.因为 ...

  4. Jquery浅析

    目录 jquery 通过jquery改变标签字体颜色 jquery和js对象之间值转化 Jquery基本选择器 Jquery层级选择器 基本筛选器 操作类属性 模太框 表单筛选器 筛选器方法 设置多个 ...

  5. swiper在vue中的用法

    首先通过npm i vue-awesome-swiper --save 来在vue中下载插件 然后再main.js中引入 require('swiper/dist/css/swiper.css')im ...

  6. ECMAScript 2015 迭代器协议:实现自定义迭代器

    迭代器协议定义了一种标准的方式来产生一个有限或无限序列的值,并且当所有的值都已经被迭代后,就会有一个默认的返回值. 当一个对象只有满足下述条件才会被认为是一个迭代器:它实现了一个 next() 的方法 ...

  7. Android深度探索-卷1第七章心得体会

    创建LED驱动的设备文件 第一步:使用cdev_init函数初始化cdev 第二步:指定设备号.直接在代码指定或动态分配 第三步:使用cdev_add函数将字符设备添加到内核中的字符设备数组中 第四步 ...

  8. 关于this、Echarts中的data

    this是指当前对象 移除class的jQuery代码:$('ur.nav li:eq(0)').removeClass('active') 添加class的jQuery代码:$('ur.nav li ...

  9. php难不难?

    php难不难?多久能学会? 我认为php难不难学和php多久学会是一个共性问题,所以我们首先来总结下有那么几种情况. 好的情况: 1.不排除有的人有天赋.智商高脑仁大.上手很快,这个和脑仁中的Z字回形 ...

  10. CentOS删除Applications中的菜单项

    有时候会错误的安装一些软件,可能安装被不成功,但是在左上角的Applications菜单中还是会显示出来,让人很不爽. 现在介绍一个删除掉CentOS Applications中菜单项的方法: 1.安 ...