我正在实现JEE7 Web应用程序。在工作期间,我发现处理自定义异常存在问题。

我修改了帐户的属性,使其具有非唯一的登录字段。然后,我调用AccountEditBean#editAccount()来运行编辑过程。当进程进入AccountFacade#edit()时,我可以看到(在调试中)PersistenceException被捕获,并且抛出了我的自定义NonUniqueException。问题是,异常不会传播到Facade类之外,并且不会在AccountEditBean中处理。而不是在TransactionalException之后立即出现throw

WARNING:   EJB5184:A system exception occurred during an invocation on
EJB ADMEndpoint, method: public void
pl.rozart.greatidea.adm.endpoints.ADMEndpoint.editAccount(pl.rozart.greatidea.entities.Account)
throws pl.rozart.greatidea.exceptions.BaseApplicationException
WARNING:   javax.transaction.TransactionalException: Managed bean with
Transactional annotation and TxType of REQUIRES_NEW encountered
exception during commit javax.transaction.RollbackException:
Transaction marked for rollback.


附加信息:
NonUniqueException扩展了BaseApplicationException,它被标记为@ApplicationException(rollback=true)

这是编辑过程的代码:

AccountEditBean:

@Named(value = "accountEditBean")
@ViewScoped
public class AccountEditBean extends UtilityBean implements Serializable {

    @Inject
    ADMEndpointLocal admEndpoint;

    private Account account;

    public void editAccount() {
        try {
            admEndpoint.editAccount(this.account);
            Messages.addInfo(ACCOUNT_DETAILS_FORM, KEY_CHANGES_SUCCESS);
        } catch (NonUniqueException e) {
            Messages.addError(ACCOUNT_DETAILS_FORM, e.getMessage());
        } catch (BaseApplicationException e) {
            Messages.addFatal(ACCOUNT_DETAILS_FORM, e.getMessage());
        }
    }

}


ADM端点:

@Stateful
@Transactional(Transactional.TxType.REQUIRES_NEW)
@TransactionTracker
public class ADMEndpoint extends LoggingStateBean implements ADMEndpointLocal, SessionSynchronization {

    @EJB(name = "ADMAccountFacade")
    private AccountFacadeLocal accountFacade;

    private Account account;

    @Override
    public void editAccount(Account account) throws BaseApplicationException {
        this.account.setLogin(account.getLogin());
        this.account.setEmail(account.getEmail());
        accountFacade.edit(this.account);
    }

}


ADMAccountFacade:

@Stateless(name = "ADMAccountFacade")
@Transactional(Transactional.TxType.MANDATORY)
@TransactionTracker
public class AccountFacade extends AbstractFacade<Account> implements AccountFacadeLocal {

    @PersistenceContext(unitName = "myPU")
    private EntityManager em;

    @Override
    public void edit(Account account) throws BaseApplicationException {
        try {
            em.merge(account);
            em.flush();
        } catch (PersistenceException e){
            if(e.getMessage().contains(Account.CONSTRAINT_ACCOUNT_LOGIN_UNIQUE)){
                throw new NonUniqueException(NonUniqueException.MSG_NON_UNIQUE_ACCOUNT_LOGIN, e);
            }else{
                throw new BaseDatabaseException(BaseDatabaseException.MSG_GENERAL_DATABASE_ERROR, e);
            }
        }
    }
}


您知道造成问题的原因吗?除所有自定义例外外,它都出现在我的所有立面上。

最佳答案

我认为您应该将@Transactional更改为@TransactionAttribute,因为带有EJB的注释。 @Transactional放在Java 7中而不是EJB中的ManagedBean上...

我在这里复制了我的评论,因为我没有足够的要浪费的地方了:)

09-05 20:51