gpt4 book ai didi

java - EJB容器中RuntimeException和@ApplicationException(rollback=true)Exception的区别

转载 作者:搜寻专家 更新时间:2023-10-31 19:47:37 25 4
gpt4 key购买 nike

我在 JSF bean 中这样调用 account EJB 方法:

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 !");
}

我的异常(exception):

public class SimRuntimeException extends RuntimeException {

public SimRuntimeException() {
super();
}

}



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

public SimNotRuntimeException() {
super();
}


}
  • account.someFunction(...); 抛出 SimRuntimeException 我摔倒了进入 Exception block ,因为我的 SimRuntimeException 被包装了可能通过 EJB 容器进入 EJBException
  • account.someFunction(...); 抛出 SimNotRuntimeException 我按预期跌倒进入 SimNotRuntimeException

那么,请问 Exception@ApplicationException(rollback=true)RuntimeException 之间的具体区别是什么?

最佳答案

Pal's blog状态:

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.

关于java - EJB容器中RuntimeException和@ApplicationException(rollback=true)Exception的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14263534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com