为什么我无法捕获此EJBTransactionRolledbackException

时间:2016-07-07 16:00:54

标签: jpa jboss ejb

服务器:WildFly10 JPA与eclipseLink 2.6.3-M1 JavaEE7

我有以下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. 首先,从db
  2. 获取tws14的实例
  3. 找到tws14的实例:za.co.fnds.persistence.entities.Tws14WSBatchChRequests [batchChRequestId = 103] 2.1 CARD PLASTIC:NBCRFLI_PIN
  4. 现在调用抛出EJBTrxnRollBackException ....
  5. 的方法
  6. 在tws14上设置一些无效值,试图抛出异常......

    --->如果抛出异常,则不应记录此行....

  7. 最后再次尝试从数据库中获取新的实例...
  8. 能够从db获取实例:za.co.fnds.persistence.entities.Tws14WSBatchChRequests [batchChRequestId = 103]
  9. 再次尝试并更新tws的实例......
  10. 可以毫无问题地更新实例.....
  11. 检查OrderCards值:
  12. 完成...
  13. 我的问题是为什么程序不会进入catch子句,因为我的日志表明抛出了javax.validation.ConstraintViolationException。为什么上面的粗体日志仍然显示?我错过了什么?我有没有办法在EJB中处理这个程序结构?

1 个答案:

答案 0 :(得分: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();
}

如果异常正在捕获,那么您的代码不会抛出任何内容