EJB 3.0 how can I wrap an exception so that the client can handle it?
Got Ejb
@Stateless
@Local
public class UserManagerBean implements UserManagerBeanLocal {
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Long registerUser(User user) throws UserAlreadyExistException {
开发者_StackOverflow社区 return userManagerDao.createUser(user);
}
}
UserManagerDao catch database exception (or all Exceptions) and re throw it to UserManagerBean.
@ApplicationException(rollback=true)
public class UserAlreadyExistException extends Exception {
}
For some reason this exception is wrapped to EJBException and throw to client. That's what I see on client side.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.ejb.EJBException: Transaction aborted
javax.ejb.EJBException: Transaction aborted
javax.transaction.RollbackException: Transaction marked for rollback.
root cause
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DatabaseException
What I want to achieve is to catch application exception(UserAlreadyExistException) on the client side, but container wrap it and i can't
Just started to learn j2ee6. Using glassfish 3.0. Am I missing something ?
Update
Sorry, i realized what was the problem, i was catching and re throwing exception on EntityManager.persist method in my dao-layer, which actually did not throw any exception, since persistence contexted was flushed on method end, not on persist invocation. This mean i did not even catch this exception
But actually it does not solve my problem, i don't understand where should i re throw exception, since both method are transactional.
You should call flush method of EntityManager during the transaction to catch DatabaseException and throw your own exception..
精彩评论