2017-08-09 147 views
1

EJB容器认为异常有两种方式 -JAVA EE - EJB/CDI/JPA:异常处理

  • 应用程序异常 - 如果违反业务规则或在执行业务逻辑出现异常。

  • 系统异常 - 任何异常,这不是由业务逻辑或业务代码引起的。 RuntimeException,RemoteException是SystemException。例如,在EJB查找期间发生错误。 RuntimeException,RemoteException是SystemException。

- >这是否意味着我需要使用checked异常我bussines逻辑是什么?喜欢这个 ?

private void checkConstraints(Object object) throws ValidationException{ 
    Set<ConstraintViolation<Object>> constraintsAdress = this.getValidator().validate(object); 
    if(!constraintsAdress.isEmpty()){ 
     String fullErrorConstraint = ""; 
     for (ConstraintViolation<Object> constraint : constraintsAdress) { 
      fullErrorConstraint = fullErrorConstraint + constraint.getMessage() + "\n"; 
     } 
     throw new ValidationException(fullErrorConstraint); 
    } 
} 

@Override 
public long addCafe(Cafe cafe) throws ValidationException, DBException{ 
    this.checkConstraints(cafe.getAddress()); 
    for(FootballMatch footballMatch: cafe.getNextMatchesToWatch()){ 
     this.checkConstraints(footballMatch); 
    } 
    this.checkConstraints(cafe); 
    this.getManager().persist(cafe); 
    return cafe.getCafeID(); 
} 

但是......

应用程序异常不会自动导致除非ApplicationException的注释应用到异常类,并与回滚元素值true指定标记事务回滚...

我不完全理解它了...... 这是个好主意,用:

  • @ApplicationException(rollback = true)?
  • 你可以使用这一个,然后也作出unchecked异常?

THX提前 干杯 汤姆

回答

1
  • IMO @ApplicationException(回滚=真)是一个好主意,从此容器的行为定义。一旦发生这种异常,回滚是确定的。
  • 您也可以将未经检查的异常声明为ApplicationExceptions。

如果你有兴趣,我比较tomee的行为,并wildfly使用一些测试中: Baseclass-Bean for test

实际testclasses是:

arquillian/wildflytomee/embedded

一些测试被停用因为tomee似乎不能正确支持“继承”。