@Transactional rollbackFor scope

huangapple go评论71阅读模式
英文:

@Transactional rollbackFor scope

问题

我在代码中使用了@Transactional,并创建了一个自定义异常,以便以特定格式在用户界面显示错误消息。

public class MyCustomException extends RuntimeException

当遇到此异常时,我仍希望像其他任何异常发生的情况一样回滚我的事务。

因此,为了使其工作,我编写了以下代码:

// 从 REST 控制器调用的服务方法
public List<String> getMyData() { 
    
    List<String> errors = new ArrayList();
    try {
        businessMethod();
    } catch (MyCustomException e) {
        log.error(e.getMessage);
        errors.add(e.getMessage)
    }
    return errors.
}

@Transactional(rollbackFor = {MyCustomException.class, RuntimeException.class, Exception.class})
public String businessMethod() {
    // 获取可能抛出 MyCustomException 的数据的业务逻辑
}

我的问题是:

  1. 如果我在rollbackFor中提及了MyCustomException.class,是否还需要提及RuntimeException.class, Exception.class?或者在rollbackFor中提到的内容会与默认的异常一起追加,以进行事务回滚。

  2. 尽管我从businessMethod()中逃逸了MyCustomException,但我在其调用方法getMyData()中捕获了它。我假设在发生异常的情况下事务会回滚,正确吗?

英文:

I'm using @Transactional in my code and I'm created a custom exception to show error messages in specific format in UI.

public class MyCustomException extends RuntimeException

When this exception is encountered I still want to rollback my transactions, same as in case when any other exception occurs.

So to make it work, I writing below code:

// service method called from rest controller
public List&lt;String&gt; getMyData() { 
	
	List&lt;String&gt; errors = new ArrayList();
	try {
		businessMethod();
	} catch (MyCustomException e) {
		log.error(e.getMessage);
		errors.add(e.getMessage)
	}
return errors.
}

@Transactional(rollbackFor = {MyCustomException.class, RuntimeException.class, Exception.class})
public String businessMethod() {
	// Business logic to get data that can throw MyCustomException
}

My questions are:

  1. If I'm mentioning MyCustomException.class in rollbackFor, do I need to also mention RuntimeException.class, Exception.class. Or whatever is mentioned in rollbackFor gets appended along with default exceptions for which transaction is rolled-back.

  2. Although I'm escaping the MyCustomException from businessMethod(), but I'm catching it on its calling method getMyData(). I'm assuming that the transaction will be rolled-back in case of exception, correct?

答案1

得分: 1

  1. 在任何“RuntimeException”情况下,事务将被回滚,因此在“rollbackFor”部分中声明您自己的“MyException.class”是不必要的,因为您的“MyException”扩展了“RuntimeException”。如果您声明“Exception.class”,则将在任何异常情况下执行回滚。但在您的情况下,您根本不需要“rollbackFor”。

  2. 是的,这是正确的。您的事务在“businessMethod()”中启动并结束。

英文:
  1. The transaction will be rolled back on any RuntimeException, so it is not necessary to declare your own MyException.class in rollbackFor section, since your MyException extends RuntimeException. If you declare Exception.class the rollback will be performed on any Exception. But in your case you do not need rollbackFor at all.

  2. Yes, it is correct. Your transcation starts and ends in businessMethod().

huangapple
  • 本文由 发表于 2020年8月16日 18:54:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63435988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定