Spring Transaction Propagation
原创转载请注明出处: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://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial
Spring Transaction Propagation的更多相关文章
- spring Transaction Propagation 事务传播
spring Transaction中有一个很重要的属性:Propagation.主要用来配置当前需要执行的方法,与当前是否有transaction之间的关系. 我晓得有点儿抽象,这也是为什么我想要写 ...
- Spring Transaction属性之Propagation
spring Transaction中有一个很重要的属性:Propagation.主要用来配置当前需要执行的方法,与当前是否有transaction之间的关系. 我晓得有点儿抽象,这也是为什么我想要写 ...
- spring Transaction Management --官方
原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12. ...
- springboot成神之——spring boot,spring jdbc和spring transaction的使用
本文介绍spring boot,spring jdbc和spring transaction的使用 项目结构 依赖 application model层 mapper层 dao层 exception层 ...
- spring transaction 初识
spring 事务初识 1.spring事务的主要接口,首先盗图一张,展示出spring 事务的相关接口.Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibern ...
- Spring Transaction 使用入门
一.开篇陈述 1.1 写文缘由 最近在系统学习spring框架IoC.AOP.Transaction相关的知识点,准备写三篇随笔记录学习过程中的感悟.这是第一篇,记录spring Transactio ...
- 疑惑的 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L
在MAVEN项目里面,在整合spring和mybatis在执行数据库操作的时候报出了: java.lang.AbstractMethodError: org.mybatis.spring.transa ...
- Spring AOP propagation七种属性值
<!-- 配置事务通知 --> <tx:advice id="advice" transaction-manager="transactionManag ...
- 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 ...
随机推荐
- 解决Windows2003 Server终端服务120天限制
用过windows server 2003做服务器的人都知道,windows2003的性能安全性比以前的windows版本高出很多,但是也带来很多麻烦.其中服务器最重要的远程管理“终端服务”居然要求授 ...
- 20180711-Java分支结构 – if…else/switch
public class Test{ public static void main(String args[]){ int x = 10; if(x<20){ System.out .prin ...
- spring4.1.8扩展实战之八:Import注解
spring4.1.8扩展实战之八:Import注解 2018年09月10日 12:53:57 博陵精骑 阅读数:441更多 所属专栏: spring4源码分析与实战 版权声明:欢迎转载,请注明 ...
- redis主从与集群搭建
redis搭建主从 条件:yum安装(3.2.1)与编译安装(5.0.0)都可以 环境:我这里在同一台主机上搭建,当然也可以两台. 1) 复制redis.conf的主配置文件并命令为slave.con ...
- Vagrant 手册之 Vagrantfile - 概述
原文地址 Vagrantfile 的主要用途是描述用于项目的机器类型,以及如何配置和提供这些机器. Vagrant 的每个项目运行一个 Vagrantfile,并且 Vagrantfile 应该被提交 ...
- Bootstrap 学习笔记13 附加导航插件
附加导航代码: <style> a:focus { outline: none; } .nav-pills { width: 150px; } .nav-pills.affix { top ...
- mooc-IDEA 调试代码--012
mooc-IDEA 调试代码 添加断点快捷键:ctrl+F8 单步运行:F9 <=>resum(从一个断点跳转到下一个断点) 一行一行运行:F8 查看所有断点: 禁止所有断点: 条件断点 ...
- 单例模式(Singleton Patten)
顾名思义,单例模式就是只有一个实例,不管怎样,使用了单例模式的类在系统中只有一个对象被访问到.Java中单例模式定义:“一个类有且仅有一个实例,并且这个类会自行实例化,实例化时候的对象可以提供给整个系 ...
- springboot异步任务、定时任务
打开浏览器 http://localhost:8080/hello ,连续刷新可以看到不会 等待 3秒时间了,pom.xml controller service 代码如下. -----------S ...
- maven基础--IDEA集成
创建项目 构建项目 查找依赖 依赖范围 provided:已提供依赖范围.编译和测试有效,运行无效.如servlet-api,在项目运行时,tomcat等容器已经提供