JPA的事务注解@Transactional使用总结
在项目开发过程中,如果您的项目中使用了Spring的@Transactional注解,有时候会出现一些奇怪的问题,例如:
明明抛了异常却不回滚?
嵌套事务执行报错?
...等等
很多的问题都是没有全面了解@Transactional的正确使用而导致的,下面一段代码就可以让你完全明白@Transactional到底该怎么用。
直接上代码,请细细品味
@Service
public class SysConfigService { @Autowired
private SysConfigRepository sysConfigRepository; public SysConfigEntity getSysConfig(String keyName) {
SysConfigEntity entity = sysConfigRepository.findOne(keyName);
return entity;
} public SysConfigEntity saveSysConfig(SysConfigEntity entity) { if(entity.getCreateTime()==null){
entity.setCreateTime(new Date());
} return sysConfigRepository.save(entity); } @Transactional
public void testSysConfig(SysConfigEntity entity) throws Exception {
//不会回滚
this.saveSysConfig(entity);
throw new Exception("sysconfig error"); } @Transactional(rollbackFor = Exception.class)
public void testSysConfig1(SysConfigEntity entity) throws Exception {
//会回滚
this.saveSysConfig(entity);
throw new Exception("sysconfig error"); } @Transactional
public void testSysConfig2(SysConfigEntity entity) throws Exception {
//会回滚
this.saveSysConfig(entity);
throw new RuntimeException("sysconfig error"); } @Transactional
public void testSysConfig3(SysConfigEntity entity) throws Exception {
//事务仍然会被提交
this.testSysConfig4(entity);
throw new Exception("sysconfig error");
} @Transactional(rollbackFor = Exception.class)
public void testSysConfig4(SysConfigEntity entity) throws Exception { this.saveSysConfig(entity);
} }
总结如下:
/**
* @Transactional事务使用总结:
*
* 1、异常在A方法内抛出,则A方法就得加注解
* 2、多个方法嵌套调用,如果都有 @Transactional 注解,则产生事务传递,默认 Propagation.REQUIRED
* 3、如果注解上只写 @Transactional 默认只对 RuntimeException 回滚,而非 Exception 进行回滚
* 如果要对 checked Exceptions 进行回滚,则需要 @Transactional(rollbackFor = Exception.class)
*
* org.springframework.orm.jpa.JpaTransactionManager
*
* org.springframework.jdbc.datasource.DataSourceTransactionManager
*
* org.springframework.transaction.jta.JtaTransactionManager
*
*
*
*/
JPA的事务注解@Transactional使用总结的更多相关文章
- SpringBoot事务注解@Transactional
SpringBoot提供了非常方便的事务操作,通过注解就可以实现事务的回滚,非常方便快捷,下面我们就说一下如何进行事务操作. 1. 事务说明 在Spring中,事务有两种实现方式,分别是编程式事务管理 ...
- Spring学习之事务注解@Transactional
今天学习spring中的事务注解,在学习Spring注解事务之前需要明白一些事务的基本概念: 事务:并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通 ...
- Springboot 事务注解--- @Transactional
spring boot @Transactional事物处理 spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransacti ...
- 《四 spring源码》spring的事务注解@Transactional 原理分析
先了解什么是注解 注解 Jdk1.5新增新技术,注解.很多框架为了简化代码,都会提供有些注解.可以理解为插件,是代码级别的插件,在类的方法上写:@XXX,就是在代码上插入了一个插件. 注解不会也不能影 ...
- Spring事务注解@Transactional回滚问题
Spring配置文件,声明事务时,如果rollback-for属性没有指定异常或者默认不写:经测试事务只回滚运行时异常(RuntimeException)和错误(Error). <!-- 配置事 ...
- Spring 事务注解@Transactional
事务管理一般有编程式和声明式两种,编程式是直接在代码中进行编写事物处理过程,而声名式则是通过注解方式或者是在xml文件中进行配置,相对编程式很方便. 而注解方式通过@Transactional 是常见 ...
- Spring事务注解@Transactional失效的问题
在项目中发现事务失效,使用@Transactional注解标注的Service业务层实现类方法全部不能回滚事务了,最终发现使用因为Spring与shiro进行整合之后导致的问题,将所有的Service ...
- 为什么阿里规定需要在事务注解@Transactional中指定rollbackFor?
作者:Mint6 来源:http://39sd.cn/53D5D Java阿里巴巴规范提示:方法[edit]需要在Transactional注解指定rollbackFor或者在方法中显示的rollba ...
- Spring事务管理者与Spring事务注解--声明式事务
1.在Spring的applicationContext.xml中配置事务管理者 PS:具体的说明请看代码中的注释 Xml代码: <!-- 声明式事务管理的配置 --> <!-- 添 ...
随机推荐
- 遇到的check the manual that corresponds to your MySQL server version for the right syntax错误
遇到的check the manual that corresponds to your MySQL server version for the right syntax错误. 结果发现是SQL关键 ...
- Verilog之SOS信号-仿顺序操作
SOS信号:. . . _ _ _ . . . 1. module sos_module ( CLK, RSTn, Pin_Out, SOS_En_Sig ); input CLK; input RS ...
- 网站建设中前端常用的jQuery+easing缓动的动画
网站建设中前端人员利用jQuery实现动画再简单不过了,只是要实现更酷的效果还需要插件来帮忙,easing就是一款帮助jQuery实现缓动动画的插件,经过试用,效果很不错! 下载该插件:jquery. ...
- checkbox和radio使用
jQuery获取Radio选择的Value值: jQuery C# VB C++ Java jQuery设置Radio的Value值: 语法解释: 1. $("input[n ...
- asp控件Repeater运用
双层repeater嵌套 <asp:Repeater ID="rpt_dataRepeatgroup" runat="server" OnItemData ...
- ArcGIS Engine代码共享-工作空间(workspace)对象操作
代码: public class WorkspaceHelper { public static string GISConnectionString; public static IWorkspac ...
- 4.27-4.30webstorm
本周学习了html的基础课程,运用的软件是webstorm,网页的结构大体为: <html><head> My First Heading </head> < ...
- WPF 虚拟键盘
之前做了一款WPF虚拟键盘,调用Win32的API,可以模拟键盘事件. 现将代码分享如下: 按键布局如下: <Button Name="> <StackPanel Orie ...
- VS20xx IDE开发应用时_拷贝VS环境的库文件DLL到目标设备上运行的操作步骤
问题场景: 当用VSIDE开发完一个项目,在部署到目标环境中时,配置文件OK,但是报错需要调试 <1>Deploying Visual C++ library DLLs as shared ...
- 从0开始学Swift笔记整理(五)
这是跟在上一篇博文后续内容: --Core Foundation框架 Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.虽然在Sw ...