5

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 ?

4

1 回答 1

7

Pal 的博客指出:

EJB 在应用程序异常和系统异常方面有所不同。应用程序异常是您定义、抛出并且您知道的东西。默认情况下,应用程序异常不会导致回滚,除非您以这种方式定义它(我认为这是推荐的)。方法签名中提到的每个已检查异常以及使用 @ApplicationException 注释的任何已检查或未检查异常都是应用程序异常。

系统异常发生在您无法控制的情况下,它们是未经检查的异常。它们总是导致回滚。好的做法是,如果您在方法中将检查的异常(这是无法避免的)包装到 EJBException 中,例如 ParseException。

于 2015-10-20T19:45:53.100 回答