gpt4 book ai didi

java - 抛出应用程序异常会导致TransactionalException

转载 作者:行者123 更新时间:2023-12-01 13:34:53 25 4
gpt4 key购买 nike

我正在实现一个 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);
}

}

ADMAAccountFacade:

@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);
}
}
}
}

您知道问题的原因是什么吗?它出现在我的每个立面中,除了所有自定义的异常(exception)。

最佳答案

我认为您应该将@Transactional更改为@TransactionAttribute,因为EJB用它进行了注释。 @Transactional 放在 java 7 中的托管bean 上,而不是 EJB 中...

我在这里复制了我的评论,因为我没有足够的积分来浪费:)

关于java - 抛出应用程序异常会导致TransactionalException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21363423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com