以下整理自己在使用Spring(3.1.1)注解式声明事务遇到的相关问题。
1、
方法的访问权限只有在public时,@Transactional才会生效。
引用
摘自
http://docs.spring.io/spring/docs/3.2.12.RELEASE/spring-framework-reference/htmlsingle/
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
2、
同一个类中都声明@Transactional的方法调用,第二个方法的@Transactional不会生效。
引用
3、
嵌套事务(子事务以REQUIRES_NEW方式)造成数据库死锁
引用
摘自
http://stackoverflow.com/questions/17747906/getting-deadlock-found-when-trying-to-get-lock-try-restarting-transaction
MySQL's InnoDB engine sports row-level locking, which can lead to deadlocks even when your code is inserting or updating a single row (specially if there are several indexes on the table being updated). Your best bet is to design the code around this in order to retry a transaction if it fails due to a deadlock. Some useful info about MySQL deadlock diagnose and possible workarounds is available here.
An interesting implementation of deadlock retry via AOP in Spring is available here. This way you just need to add the annotation to the method you want to retry in case of deadlock.
4、
在事务提交\事务回滚之后,处理一些事情。
// 在业务代码中,调用此代码
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter(){
// 重写afterCommit或者afterCompletion
// 在事务提交\事务回滚之后,就会执行
});
分享到:
相关推荐
NULL 博文链接:https://tonl.iteye.com/blog/2093289
在Spring3中配置声明式事务比早期版本显得更加简便。只需要几行配置文件+注解就可以实现面向切面的AOP事务
Spring 整合mybatis(注解&xml版声明式事务).pdf
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
5.2.2 基于Annotation方式的声明式事务 基于Annotation方式的声明式事务 1 在Spring容器中注册事务注解驱动; 2 在需要事务管理的类或方法上使用@Transactional注解。 ...
Spring通过@Transactional注解以及底层的AOP和代理机制实现了声明式事务。这个机制允许开发者通过简单的注解就能控制事务的边界和规则,极大地简化了编程模型。在Spring中,事务管理是通过一系列的拦截器和事务管理...
spring boot 纯注解方法事务控制回滚,注解+简单配置文件使用多线程demo
Spring+Hibernate注解声明式事务实例
声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,更清爽。spring注解事务实现demo
等事务 API,实现了一致的编程模型,而 Spring 的声明式事务功能更是提供了极其方便的事务配置方式,配合 Spring Boot 的自动配置,大多数 Spring Boot 项目只需要在方法上标记 @Transactional 注解,即可一键开启...
注解实现声明式事务管理
Spring注解驱动开发第35讲——声明式事务原理的源码分析
一个完整的s2sm整合项目,含jar包。使用注释型事务管理。给大家参考了。
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
主要介绍了Spring注解 - TX 声明式事务实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring.net 声明式事物管理小示例,有完整的配置,以及测试
(2)(2) 在BookShopDao中添加一个purchase购书方法,其操作流程是获取书的单价->买书(更新库存)->更新账户余额,在BookShopDaoImp中实现该方法,并使用基于XML和Annotation的声明式事务管理来确保该购书过程能正常...
主要介绍了Spring实战之使用注解实现声明式事务操作,结合实例形式详细分析了spring使用注解实现声明式事务相关配置、接口实现与使用技巧,需要的朋友可以参考下
Spring提供了两种事务管理方式:编程式事务管理和声明式事务管理; 描述了声明式事务管理,XML,注解;事务配置详解。。。
事务简介 二、程序举例环境搭建 o1. 创建数据表 o2....o3....o4....o5....o6....o7....o8....o9. Spring配置文件 ... 声明事务管理器 o2. 开启注解驱动 o3. 完整Spring配置文件 o4. 业务层 public 方法加入事务属性 o5. 测试