-
15. Re: JBoss 4.0.3 - EJB 3.0 - Exception caused by UNIQUE const
cbosquet Nov 9, 2005 1:24 PM (in response to jonefun)I ask you if try to annotate your checked Exception because in org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx JBoss source code there is check that verify if the ApplicationException annotation is present :
public void handleExceptionInOurTx(Invocation invocation, Throwable t, Transaction tx) throws Throwable { if (t.getClass().isAnnotationPresent(ApplicationException.class)) { ApplicationException ae = (ApplicationException) t.getClass().getAnnotation(ApplicationException.class); if (ae.rollback()) setRollbackOnly(tx); throw t; } if (!(t instanceof RuntimeException || t instanceof RemoteException)) { throw t; } setRollbackOnly(tx); if (t instanceof RuntimeException && !(t instanceof EJBException)) { throw new EJBException((Exception) t); } throw t; }
Charles -
16. Re: JBoss 4.0.3 - EJB 3.0 - Exception caused by UNIQUE const
jonefun Nov 10, 2005 6:29 AM (in response to jonefun)Excellent :) That's nailed the sucker !!!!
As you rightly pointed out Charles the absence of @ApplicationException annotation causes the underlying RuntimeException.
Lessons learned :-
(1) - To force the checked exception in your Session Bean you must manually Flush the EntityManager at this point the transaction is committed and any exceptions will be gratefully received ( Thanks Emmanuel )
(2) - When throwing a checked exception inside your session bean ensure that you annotate it accordingly. This is important because JBoss needs to recognise that this is an application checked exception and not an unchecked runtime exception. Jboss will then deal with the exception accordingly. In my case this means JBoss throws my nice PersonAlreadyExistsException and not the nasty RuntimeException. ( Thanks Charles ) -
17. Re: JBoss 4.0.3 - EJB 3.0 - Exception caused by UNIQUE const
trofimov Dec 15, 2005 10:49 PM (in response to jonefun)I have a similar problem.
My Slsb have method:public void addUser(User user) throws Exception { try { em.persist(user); em.flush(); } catch (ConstraintViolationException e) { throw new UserAlreadyExistsException( "User already exist!", e); } catch (PropertyValueException e) { throw new PropertyInvalidException(e.getEntityName(), e.getPropertyName()); } }
UserAlreadyExistsException it is caught, but if i catch PropertyValueException, PropertyInvalidException not rise!
Client recive:java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=lserver/68, BranchQual=, localId=68] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.PropertyValueException: not-null property references a null or transient value: ru.migusoft.ent.persist.security.User.accountName)
Where I should catch PropertyValueException exception? -
18. Re: JBoss 4.0.3 - EJB 3.0 - Exception caused by UNIQUE const
jonefun Dec 20, 2005 10:24 AM (in response to jonefun)Any checked exceptions raised should all be annotated with @ApplicationException so that the container does not raise them as a RuntimeException which is what is happening in this case.
Cheers
Jono -
19. Re: JBoss 4.0.3 - EJB 3.0 - Exception caused by UNIQUE const
laboratory Jan 19, 2006 8:16 AM (in response to jonefun)can't fix this problem, am i doing something wrong?
My Exception:@ApplicationException public class CreateAccountException extends Exception { public CreateAccountException(String message) { super(message); } }
My Entity Field:@Column(name="USERNAME", unique=true) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }
My SLSB Method:public void addAccount(String username, String password) throws CreateAccountException { Account account = new Account(); account.setUsername(username); account.setPassword(password); try { entityManager.persist(account); entityManager.flush(); } catch(ConstraintViolationException e) { throw new CreateAccountException("[+]ERROR CREATING ACCOUNT"); } }
My Servlet Method:protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { accountsManager.sayHello(); try { accountsManager.addAccount("a","b"); accountsManager.addAccount("a","b"); } catch (CreateAccountException e) { System.err.println(e.getMessage()); } }
-
20. Re: JBoss 4.0.3 - EJB 3.0 - Exception caused by UNIQUE const
laboratory Jan 19, 2006 8:22 AM (in response to jonefun)solved adding (rollback=true) to ApplicationException tnx anyway ;)