Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between RuntimeException and Exception with @ApplicationException(rollback=true) in EJB container

I call account EJB method in JSF bean like that :

try{
   account.someFunction(...);
}catch(SimRuntimeException e){
   logger.log(Level.FATAL, "SimRuntimeException catched !");
}catch(SimNotRuntimeException e){
   logger.log(Level.FATAL, "SimNotRuntimeException catched !");
}catch(Exception e){
   logger.log(Level.FATAL, "Exception catched !");
}

My Exceptions :

public class SimRuntimeException extends RuntimeException {

   public SimRuntimeException() {
      super();
   }

}



@ApplicationException(rollback=true)
public class SimNotRuntimeException extends Exception {

   public SimNotRuntimeException() {
      super();  
   }


}
  • when account.someFunction(...); throws SimRuntimeException I fall into Exception block because my SimRuntimeException is wrapped into EJBException probably by EJB container.
  • when account.someFunction(...); throws SimNotRuntimeException I fall as expected into SimNotRuntimeException

So, what is concretely the difference between Exception with @ApplicationException(rollback=true) and RuntimeException please ?

like image 615
Olivier J. Avatar asked Jan 10 '13 17:01

Olivier J.


1 Answers

Pal's blog states:

EJB makes a difference in Application Exceptions and System Exceptions. Application exception is something that you define, you throw, and you are aware of. By default the application exception does not cause a rollback, unless you define it that way (and I think it's recommended). Every checked exception that is mentioned in the method signature and also any checked or unchecked exception that is annotated with @ApplicationException, is an application exception.

System exceptions happen in cases, you don't control, and they are unchecked exceptions. They always cause rollback. Good practice is, if you wrap checked exceptions -- that cannot be avoided -- in your method into EJBException e.g. ParseException.

like image 158
baumato Avatar answered Sep 30 '22 17:09

baumato