回滚@Transactional注释的方法 [英] Rollback a @Transactional annotated method

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

问题描述

美好的一天。以下代码:

  class A {
private B b;
@Transactional
public SomeResult doSomething(){
SomeResult res = null;
try {
//做某事
} catch(Exception e){
res = b.saveResult();
}
return res;


@Transactional(propagation = Propagation.REQUIRES_NEW)
class B {
public SomeResult saveResult(){
// save in db


code


$ b据我所知,如果方法中存在异常 doSomething 事务不会回滚。以及如何使它滚动?并返回SomeResult

解决方案

您不应以编程方式调用Rollback。按照 docs ,是使用声明式的方法。为此,您需要注释哪些异常会触发回滚。



在您的情况中,类似这样的内容



< preclass =lang-java prettyprint-override> @Transactional(rollbackFor = {MyException.class,AnotherException.class})
public SomeResult doSomething(){
...
}

查看 @ Transaction API 和关于回滚交易



如果尽管有文档建议,您想要进行程序化回滚,那么您需要按照已经建议的方式从 TransactionAspectSupport 调用它。这是来自文档:
$ b

public void resolvePosition(){
try {
//一些业务逻辑...
catch(NoProductInStockException ex){
//以编程方式触发回滚
TransactionAspectSupport.currentTransactionStatus()。setRollbackOnly();
}
}

尽管可能存在架构错误。如果你的方法失败了,你需要抛出一个异常,你不应该期望它返回任何东西。也许你对这个方法承担了太多的责任,并且应该创建一个只对模型数据进行建模的独立模型,并且在出现问题时引发异常并回滚事务。无论如何,阅读文档。


Good day. The following code:

 class A{
     private B b;
    @Transactional
    public SomeResult doSomething(){
        SomeResult res = null;
        try {
          // do something 
        } catch (Exception e) {
            res  = b.saveResult();
        }
        return res ;
    }
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
class B{
  public SomeResult saveResult(){
      // save in db 
  }
}

As I understand, if there is an exception in the method doSomething the transaction isn't rolled back. And how to make that it rolled? and returned SomeResult

解决方案

You shouldn't call Rollback programmatically. The best way, as recommended by the docs, is to use declarative approach. To do so, you need to annotate which exceptions will trigger a Rollback.

In your case, something like this

@Transactional(rollbackFor={MyException.class, AnotherException.class})
public SomeResult doSomething(){
   ...
}

Take a look at the @Transaction API and the docs about rolling back a transaction.

If, despite the docs recommendation, you want to make a programmatic rollback, then you need to call it from TransactionAspectSupport as already suggested. This is from the docs:

public void resolvePosition() {
  try {
    // some business logic...
  } catch (NoProductInStockException ex) {
    // trigger rollback programmatically
    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  }
}

There may be a architecture mistake though. If your method fails and you need to throw an exception, you shouldn't expect it to return anything. Maybe you're giving too much responsibilities to this method and should create a separated one that only model data, and throws an exception if something goes wrong, rolling back the transaction. Anyway, read the docs.

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

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