【问题标题】:how to do exception handling in container managed JTA transaction如何在容器管理的 JTA 事务中进行异常处理
【发布时间】:2015-03-13 13:34:06
【问题描述】:

这是我的代码的一部分。我正在使用 JTA 事务,这段代码抛出了唯一约束异常。

@TransactionAttribute( REQUIRED )
            private int createProfileHelper(AccountBean  accountInfo) throws Exception{    
        Long portfolio_customer_id = portfolioCustomerEntity.getId();
                            ExternalAccountEntity externalAccountEntity = new ExternalAccountEntity();
                            externalAccountEntity.setAccountNumber(accountInfo.getAccountNumber().toUpperCase());
                            externalAccountEntity.setBrand(brand);
                            externalAccountEntity.setAccountName(accountInfo.getAccountName());
                            externalAccountEntity.setRepId(accountInfo.getRepId());
                            externalAccountEntity.setCreatedBy(userName);
                            externalAccountEntity.setCreatedDate(new Date());
                            externalAccountEntity.setUserId(userId);
                            externalAccountEntity.setCustomerId(portfolio_customer_id); //join created between external account and portfolio_customer
                            persistenceToolsEntityManager.persist(externalAccountEntity);
    }

我写了这段代码来处理异常:

public int createProfile(AccountBean  accountInfo){
        try{
            return createProfileHelper(accountInfo);
        }catch(Exception e){
            logger.error(e);
            logger.error(e.getMessage());
            return 0;
        }
    }

令我惊讶的是,尽管我可以看到服务器上闪烁的异常,但我无法在我的 try catch 块中捕获异常:

Mar 13, 2015 9:19:58 AM org.apache.geronimo.transaction.manager.TransactionImpl beforeCompletion
WARNING: Unexpected exception from beforeCompletion; transaction will roll back
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-00001: unique constraint (SEC.PORTFOLIO_EXTERNAL_ACCOUNT_U1) violated

【问题讨论】:

  • 我认为异常仍然被捕获,尽管事务被回滚。你确定不是这样吗?
  • 是的 :( 我在 catch 里面放了一个断点,我无法命中它

标签: java jta apache-tomee


【解决方案1】:

真的没有那么神奇。

您的@TransactionAttribute( REQUIRED ) 实际上没有任何作用。此注解只能应用于公共方法,并且仅在从不同的 bean 调用时才有效。换句话说,您不能使用注解来划分同一个 bean 中方法调用之间的事务。

javax.persistence.PersistenceException 在退出 createProfile 方法(默认情况下具有隐式 @TransactionAttribute( REQUIRED ))时已被事务管理器捕获。

【讨论】:

  • 之前我从未创建过私有方法。我刚刚创建了它,以便我可以通过具有 @TransactionAttribute(REQUIRED) 的公共方法捕获异常。无论如何,我怎样才能将我的多个数据库调用作为单个事务进行?...这个公共方法由休息调用调用。
  • 在 REST 处理程序中捕获异常。即调用createProfile的代码
【解决方案2】:

尝试在persistenceToolsEntityManager.persist 上下断点并逐行进入代码。
您会发现org.apache.geronimo.transaction.manager.TransactionImpl 会为您处理异常。
如果您想在发生任何异常时执行其他操作,请尝试使用TransactionManagergetStatus()
:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 2012-07-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多