Why session bean method throw EjbTransactionRolledbackException when RuntimeException was thrown

前端 未结 2 1040
刺人心
刺人心 2021-02-10 05:38

I am trying to persist the entity with constraint validation, when invoke persist - there is constraint that thrown and the caller get EjbTransactionRolledbackException

2条回答
  •  别跟我提以往
    2021-02-10 06:09

    so I try to call the validation explicit and throw ConstraintViolationException/RuntimeException and still the caller get EjbTransactionRolledbackException...

    Providing the full stacktrace might help. Anyway, I wonder how you are calling your EJB and if you're propagating a transaction, in which case throwing a EJBTransactionRolledbackException is the right behavior in case of a system exception. But the following blog post might help:

    Constraint violated, transaction rolled back

    When using bean validation on JPA entities within an EJB 3 bean you would actually get an EJBTransactionRolledbackException if there is a constraint violation.

    javax.ejb.EJBTransactionRolledbackException: Invalid object at persist time for groups [javax.validation.groups.Default, ]
    Caused by: javax.validation.ConstraintViolationException: Invalid object at persist time for groups [javax.validation.groups.Default, ]
    

    This is all nicely according to specification, but not really interesting information. You don't really want to know what happened, you want to know what went wrong.

    So I recommend adding the following to your ejb-jar.xml:

    
    
       
          
             javax.validation.ConstraintViolationException
             true
          
       
    
    

    That way you can directly access your violations.

    Resources

    • On EJB and application vs system exception:
      • Best practices in EJB exception handling
      • 16.6. Exceptions and Transactions
    • On Bean Validation
      • Constraint violated, transaction rolled back

提交回复
热议问题