2017-08-09 71 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似乎不能正確支持「繼承」。