0

I've tried to handle exceptions in EJB tier with a CMT. I have some crud operations that can have constraint rules violated. When some violation ocurrs, the transaction is rolled back but I can't catch the exception (I understood that the commit happens at the end of the method. Am I right ?). The question is: Should I think the EJB tier as a black box and handle the exceptions in the EJB client ?

  • Possible duplicate of [JPA data access object - exception handling and rollback](https://stackoverflow.com/questions/32040779/jpa-data-access-object-exception-handling-and-rollback) – BalusC Oct 03 '17 at 06:29
  • @BalusC I've seen in your example that you marked a custom exception that inherits RuntimeException with @ApplicationException(rollback=true)". It's really needed ? – Roberto Coelho Oct 03 '17 at 07:04
  • 1
    Only if you want to know what exactly is the problem indicated by the EJB tier. Custom exceptions are better than catching overly generic exceptions such as EJBException and perhaps parsing the message/cause/etc in order to figure out the real problem. It decouples your client tier from EJB tier. See also the 1st "See also" link in bottom of that answer. – BalusC Oct 03 '17 at 07:08
  • I asked that because I thought that RuntimeExceptions already roll back transactions. – Roberto Coelho Oct 03 '17 at 07:31
  • Yes but they are in turn wrapped in `EJBException`. You can't do `catch (YourSpecificRuntimeException e){}` anymore in your client. You're forced to use `catch(EJBException e){}` instead and you would need to introduce a `javax.ejb` specific dependency in your client. The `@ApplicationException` prevents that. See also https://stackoverflow.com/q/32853167 – BalusC Oct 03 '17 at 07:32
  • It would better always throws Runtime Exceptions than Application Exceptions ? – Roberto Coelho Oct 03 '17 at 07:37
  • Why exactly do you think that? – BalusC Oct 03 '17 at 07:38
  • I was thinking because all the things the container does after a Runtime Exception. Looking your comment I can create a custom exception (high level) using @ApplicationException and rollback attribute and then create many custom exceptions extending my high level class ? – Roberto Coelho Oct 03 '17 at 07:46

0 Answers0