我正在实现JEE7 Web应用程序.在工作期间,我发现处理自定义异常时遇到问题.
I am implementing an JEE7 web application. During my work i have found a problem with handling my custom exceptions.
我修改了帐户的属性,使其具有非唯一的登录字段.然后,我调用AccountEditBean#editAccount()来运行编辑过程.当进程进入AccountFacade#edit()时,我可以看到(在调试中)PersistenceException被捕获,并且抛出了我的自定义NonUniqueException.问题是,该异常不会传播到Facade类之外,并且不会在AccountEditBean中处理.而不是TransactionalException紧接在throw之后出现:
I edited my account's property to have a non-unique login field. Then i invoked the AccountEditBean#editAccount() to run the editing process. When the process comes to AccountFacade#edit() i can see (in debug) that PersistenceException is caught and my custom NonUniqueException is thrown. The problem is, the exception is not propagated out of the facade class and it is not handled in AccountEditBean. Instead of that TransactionalException occurs right after 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).
Additional information: NonUniqueException extends BaseApplicationException , which is marked as @ApplicationException(rollback=true).
这是编辑过程的代码:
AccountEditBean:
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()); } } }ADMEndpoint:
ADMEndpoint:
@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:
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); } } } }您知道什么可能是问题的原因吗?它发生在我的所有立面上,带有所有自定义例外.
Do you know what could be the cause of the problem? It occurs in every of my facades, with all the custom exceptions.
推荐答案我认为您应该将@Transactional更改为@TransactionAttribute,因为带有EJB的注释. @Transactional放在Java 7中而不是在EJB中的ManagedBean上.
I think you should change @Transactional to @TransactionAttribute because EJBs annotated with that. @Transactional is put on managedbean in java 7 not in EJBs...
我在这里复制了我的评论,因为我没有足够的要浪费的地方了:)
I copied my comment here because i do not have enough points to squander :)
更多推荐
引发应用程序异常会导致TransactionalException
发布评论