2016-07-07 28 views
0

服務器:WildFly10 JPA用的EclipseLink 2.6.3-M1 JavaEE7我爲什麼不能抓住這個EJBTransactionRolledbackException

我有以下EJB:

@Stateless 
@LocalBean 
public class HandleRollbackComponent { 

    private static Logger logger = Logger.getLogger(HandleRollbackComponent.class); 

    @EJB 
    private Tws14WSBatchChRequestsFacade tws14wsBatchChRequestsFacade; 

    public void doSomething() { 
     // first off go and fetch an instance of tws14 from the db 
     logger.debug("*************************************************"); 
     logger.debug("1. First off go and fetch an instance of tws14 from the db"); 

     String batchChReqId = "103"; 
     Tws14WSBatchChRequests tws14wsBatchChRequests = tws14wsBatchChRequestsFacade.find(new BigDecimal(batchChReqId)); 
     logger.debug("2. Found instance of tws14: " + tws14wsBatchChRequests); 
     logger.debug("2.1 CARD PLASTIC   : " + tws14wsBatchChRequests.getCardPlastic()); 

     try { 
      logger.debug("3. Now call a method that throws the EJBTrxnRollBackException...."); 
      doSomethingThatThrowsEJBTransactionRolledbackException(tws14wsBatchChRequests); 

      logger.debug("---> This line should not be logged if exception was thrown...."); 
     } catch (Exception e) { 
      logger.debug("5. Caught the exception...."); 
     } finally { 

      logger.debug("6. Finally try and get a fresh instance from the db again..."); 
      tws14wsBatchChRequests = tws14wsBatchChRequestsFacade.find(new BigDecimal(batchChReqId)); 
      logger.debug("7. Was able to get instance from db: " + tws14wsBatchChRequests); 

      logger.debug("8. Try and update the instance of tws again..."); 
      tws14wsBatchChRequestsFacade.edit(tws14wsBatchChRequests); 
      logger.debug("9. Could update the instance without problems....."); 

      logger.debug("10. Check the OrderCards value: " + tws14wsBatchChRequests.getOrderCards()); 
     } 

     logger.debug("11. Done..."); 

    } 

    public void doSomethingThatThrowsEJBTransactionRolledbackException(Tws14WSBatchChRequests tws14wsBatchChRequests) { 
     logger.debug("4. Set some invalid values on tws14 in an attempt to get exception thrown..."); 
     tws14wsBatchChRequests.setOrderCards("N"); 
     tws14wsBatchChRequests.setOrderCards(""); 
     tws14wsBatchChRequests.setCardPlastic(null); 

     tws14wsBatchChRequestsFacade.edit(tws14wsBatchChRequests); 
    } 

} 

當我打電話DoSomething的(),這是我所看到的:


  1. 首先去,並從取tws14的一個實例分貝
  2. 實測值tws14的實例:za.co.fnds.persistence.entities.Tws14WSBatchChRequests [batchChRequestId = 103] 2.1 CARD PLASTIC:NBCRFLI_PIN
  3. 現在調用拋出EJBTrxnRollBackException的方法....
  4. 坐落於tws14一些無效的值,試圖獲取拋出異常......

    --->如果異常被拋出此行應不會被記錄....

  5. 終於設法得到一個新的來自db的實例再次...
  6. 能夠從數據庫獲取實例:za.co.fnds.persistence.entities.Tws14WSBatchChRequests [batchChRequestId = 103]
  7. 嘗試和更新再次交易平臺的情況下...
  8. 可以更新沒有問題的情況下.....
  9. 檢查OrderCards值:
  10. 完成...

我的問題是,爲什麼在程序不打算進入catch子句,因爲我的日誌表明使用javax .validation.ConstraintViolationException是拋出ñ。爲什麼上面的大膽日誌仍然顯示?我錯過了什麼?有沒有一種方法我應該在EJB中處理這個程序結構?

回答

0

要驗證實現是否錯誤,請使用doSomethingThatThrowsEJBTransactionRolledbackException方法。您可以明確地拋出異常並查看導管是否工作。

public void doSomething() { 
    try { 
     doSomethingThatThrowsEJBTransactionRolledbackException(new Tws14WSBatchChRequests()); 
    } catch (Exception e) { 
     logger.warn(e.getMessage(), e); 
    } 
} 

public void doSomethingThatThrowsEJBTransactionRolledbackException(Tws14WSBatchChRequests tws14wsBatchChRequests) { 
    throw new EJBTransactionRolledbackException(); 
} 

如果異常正在迎頭趕上,那麼你的代碼是不扔任何東西

相關問題