Spring注解式声明事务相关问题

以下整理自己在使用Spring(3.1.1)注解式声明事务遇到的相关问题。

1、[b]方法的访问权限只有在public时,@Transactional才会生效。[/b]
[quote]
摘自[url]http://docs.spring.io/spring/docs/3.2.12.RELEASE/spring-framework-reference/htmlsingle/[/url]

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.
[/quote]

2、[b]同一个类中都声明@Transactional的方法调用,第二个方法的@Transactional不会生效。[/b]
[quote]
摘自[url]http://docs.spring.io/spring/docs/3.2.12.RELEASE/spring-framework-reference/htmlsingle/[/url]

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.
[/quote]

3、[b]嵌套事务(子事务以REQUIRES_NEW方式)造成数据库死锁[/b]
[quote]
摘自[url]http://stackoverflow.com/questions/17747906/getting-deadlock-found-when-trying-to-get-lock-try-restarting-transaction[/url]

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.
[/quote]

4、[b]在事务提交\事务回滚之后,处理一些事情。[/b]

// 在业务代码中,调用此代码
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter(){
// 重写afterCommit或者afterCompletion
// 在事务提交\事务回滚之后,就会执行
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值