-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Handling exceptions
PostPosted: Wed Jun 06, 2007 3:53 pm 
Newbie

Joined: Sun May 27, 2007 3:57 pm
Posts: 7
Hi, I am using jboss/jsf/hibernate. Using a facade model, making my calls from a managed bean.
i.e session bean -> called from JSF managed bean.

I would like to catch constraint excetions , but I have been only successful catching a Runtime exception withing the mangaged bean. I have tried to catch ConstaingViolationException , but it just fall through.

What would be the best practice to catch exceptions using a facade model.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 10:07 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Some exceptions are only thrown on a commit(). You mentioned you are using JBOSS so you probably are using CMT and you are not doing the commit yourself.

Is your session bean the only layer that is involved in a transaction?

Can you show the exception handling code?

ConstraintViolationException extends from RuntimeException so you should be able to catch it.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 12:01 am 
Newbie

Joined: Sun May 27, 2007 3:57 pm
Posts: 7
Hi, thx for the reply. Here is a stack trace of the exception.

javax.faces.el.EvaluationException: Exception while invoking expression #{AddressBookMB.createAddress}
at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:153)
at javax.faces.component.UICommand.broadcast(UICommand.java:86)
at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:619)
Caused by: javax.ejb.EJBException: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [address.AddressBookEntity]

at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:181)
at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:79)
at $Proxy71.createAddressBookEntry(Unknown Source)
at address.AddressBookMB.createAddress(AddressBookMB.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
... 24 more
Caused by: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [address.AddressBookEntity]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:555)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:192)
at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:175)
at address.AddressBookServiceBean.createAddressBookEntry(AddressBookServiceBean.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
... 45 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [address.AddressBookEntity]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2044)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2481)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:47)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:620)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:594)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:598)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:186)
... 60 more
Caused by: java.sql.SQLException: Duplicate entry 'jason-sigurdur' for key 3
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1124)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:676)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:251)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 74 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 12:19 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Well the reason you can't catch ConstraintViolationException is because it's been wrapped by a few others.


At the lowest level you have Hibernate's ConstraintViolationException.

Because of the usage of JPA, that exception is wrapped inside JPA's EntityExistsException

Code:
Caused by: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert:

Which in turn is wrapped inside an EJB exception by JBOSS

Code:
Caused by: javax.ejb.EJBException: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [address.AddressBookEntity]


So you kinda have to know what layers you have.

You can do

try {
...
} catch (EJBException e) {
Exception cause = e.getCause();

// You can do instanceof to check for types or multiple getcauses
// depending how deep you want to get into the root cause.
}

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 12:26 am 
Newbie

Joined: Sun May 27, 2007 3:57 pm
Posts: 7
Thx, where should be exception be caught, within the top layer 'jsf managed bean' or should this be caught withing the stateless session bean?

jason


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 12:38 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
If you catch it inside the stateless session bean you won't be able to catch exceptions thrown during a commit. You'd want to catch it outside of the session bean.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 10:45 am 
Newbie

Joined: Sun May 27, 2007 3:57 pm
Posts: 7
here is a snip of code from my ManagedBean. The exception still falls through?

jason


public void createAddress(ActionEvent ae)
{
AddressBookService abs;
abs = this.lookupAddressBookServiceBean();

try {
abs.createAddressBookEntry((String)this.getFirstName().getValue(),
(String)this.getLastName().getValue(), (String)this.getPhoneNumber().getValue());
this.facesMessage("add_form","Address successfully added.");
}
catch (EvaluationException ex)
{
this.facesMessage("add_form","Error: "+ex.getCause().getMessage());
}


resetForm();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 11:30 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
So who's calling createAddress?

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 11:23 pm 
Newbie

Joined: Sun May 27, 2007 3:57 pm
Posts: 7
The jsf Managed bean is calling the method.

We have [entity bean]-----[Stateless Session Bean]---[JSF managed Bean]

The Stateless Session bean acts as a Facade. Managing all persistence calls.

jason


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 11:27 pm 
Newbie

Joined: Sun May 27, 2007 3:57 pm
Posts: 7
I did have success catching a EJBException, as per your advise. I also noticed that it is the last 'Caused by exception in the stack trace'.

jason


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.