EJB container considers two exception types:
- System Exceptions
- Application Exceptions
- EJBException is a subclass of java.lang.RuntimeException
- They always cause a rollback in transactions.
- Runtime exceptions are unchecked exceptions, so they are thrown from the method (not need to throws or try/catch clauses)
- Way the container hands system exceptions:
- Roll back transaction
- Log exception
- Discard EJB instance
- The EJB wraps the exception into RemoteException and throws it to the client.
- If the client started the application and it was propagated to the EJB, the system exception will be rethrown as EJBTransactionRolledbackException
Application Exceptions.- errors in the business logic
- The do not cause a rollback by default. To rollback the transaction automatically it can be used the annotation @ApplicationException(rollback=true), or doing it explicity with a call the method setRollbackOnly()
- They are sent to the client directly. The container does not wrap the exception.
OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification
Hi, in some cases you are not able to prevent transaction rollback with @ApplicationException(rollback=false). When transaction will be marked as rollbackOnly. For example for unique constraint exception.
ReplyDeleteEven if it goes through exception handler and is rethrowed as @ApplicationException.
It can be bypassed by adding subtransactions (new session), but it's not a pretty solution.