Spring @Transactional并发 [英] Spring @Transactional concurrency

查看:423
本文介绍了Spring @Transactional并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


class MyService {

public void a(){
synchronized ){
b();
}
}

public void a() { synchronized(somekey) { b(); } }

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void b(){
.do DB works ...
}
}

@Transactional(propagation = Propagation.REQUIRES_NEW) public void b() { ...do DB works... } }

我的目标是

    1 - 获取钥匙



    2 - 开始交易



    3 - 提交事务



    4 - 释放键

()方法从外部,事务不工作。

When i call a() method from outside, transaction doesn't work.

有任何建议吗?

谢谢。

推荐答案

除非你使用代码编织,这不能工作。

Unless you're using code weaving, this can't work.

Spring处理事务的默认方式是通过 AOP代理。对事务方法的调用如下所示:

The default way Spring handles transactions is through AOP proxies. The call to a transactional method goes like this:

 caller --> ProxyClass.a() --> YourClass.a()

如果你在同一个对象上调用另一个方法,代理,因此没有事务行为。

If you call another method on the same object, you're not going through the proxy, so there is no transactional behaviour.

 caller --> ProxyClass.a() --> YourClass.a() --> YourClass.b()

如果不想使用AspectJ,使用 AopContext.currentProxy()

If you don't want to use AspectJ, you can get the proxy object using AopContext.currentProxy().

这篇关于Spring @Transactional并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆