简体   繁体   English

发生运行时异常时,Hibernate不会回滚

[英]Hibernate does not rollback in case of a runtime exception thrown

I have below piece of code in my project. 我的项目中包含以下代码。 An exception is thrown at line # 4 still my product details are saved. 第4行引发异常,但我的产品详细信息仍然保存。 I am having hard time to understand why does it save product details even after throwing the exception I am also trying to understand if the exception thrown at line #4 is a checked or unchecked exception ? 我很难理解为什么即使抛出异常后它仍保存产品详细信息, 我也试图理解第4行抛出的异常是已检查还是未检查的异常? If i am throwing "throw new Exception("Details don't match")" it is a Runtime exception I am assuming? 如果我抛出“抛出新异常(“细节不匹配”)),这是我假设的运行时异常?

class Product{
    @Transactional
        addDetails(){
            try{
            }
            catch (Exception e) {
               throw new Exception("Details dont match") //Line 4
            }
           productDAO.save(productDetails) 
           addAdditionalDetails(productDetails)
        }
       }

class ProductDAO {
   @Transactional
   public void save(Product productDetails){
       entitiyManager.merge(productDetails)
   }
}

I am also trying to understand if the exception thrown at line #4 is a checked or unchecked exception? 我还试图了解第4行抛出的异常是已检查还是未检查的异常?

Answer: java.lang.Exception is a checked exception. 答: java.lang.Exception是一个检查的异常。

If I am throwing "throw new Exception("Details don't match")" it is a Runtime exception I am assuming? 如果我抛出“抛出新异常(“细节不匹配”)),这是我假设的运行时异常?

Answer: No, it is not a RuntimeException . 答:不,它不是RuntimeException RuntimeException is those which extends java.lang.RuntimeException or its subclass . RuntimeException是那些扩展java.lang.RuntimeException或其subclass

In spring by Transaction is Rollback when a Runtime exception occurs. 在春季,发生Runtime异常时,“事务是回滚”。 That means any exception thrown in a transaction which extends RuntimeException or its subclass will rollback it. 这意味着在extends RuntimeException或其子类的事务中抛出的任何异常都将回滚它。 But in your case, you are throwing Exception which is not a type of RuntimeException . 但是在您的情况下,您将抛出不是RuntimeException类型的Exception

Solution: 解:

I will suggest creating a Custom exception which extends RuntimeExction and throws it. 我建议创建一个Custom异常,该异常将扩展RuntimeExction并将其引发。

class UnmatchedDetailException extends RuntimeException{
    UnmatchedDetailException(String msg){
        super(msg);
    }
}

And then throw the UnmatchedDetailException 然后抛出UnmatchedDetailException

throw new UnmatchedDetailException("Deatils not matched");

With default spring configurations, only un-checked runtime exceptions are rolled back. 使用默认的spring配置,仅回滚未检查的运行时异常。 In order to customize this configuration, rollbackFor is used as a property in the @Transactional annotation. 为了自定义此配置,在@Transactional批注中将rollbackFor用作属性。

For ex, 例如,

@Transactional(rollbackFor = { MyInvalidUserException.class, MyApplicationException.class }) public void method() throws MyInvalidUserException, MyApplicationException { ... ... } @Transactional(rollbackFor = {MyInvalidUserException.class,MyApplicationException.class})公共无效方法()抛出MyInvalidUserException,MyApplicationException {... ...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM