-->
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.  [ 9 posts ] 
Author Message
 Post subject: Exceptions thrown only when commit?
PostPosted: Wed May 30, 2007 3:55 pm 
Newbie

Joined: Sat Nov 05, 2005 3:38 pm
Posts: 7
In my webapp, object deletions happen in the following way.

Code:
public class BasicHibernateDao<T> implements BasicDao<T> {
   private Class<T> persistentClass;
   private SessionFactory sessionFactory;
   
   // blah blah blah...
   
   public void delete(T object) {
      try {
         sessionFactory.getCurrentSession().delete(object);
      } catch (ConstraintViolationException cause) {
         throw new CouldNotDeleteObjectException(cause);
         // CouldNotDeleteObjectException is catch by the client class
      }
   }
}


Since I'm using Struts2, the BasicHibernateDao' client classes are action classes. And since I don't want them to be aware of (or dependent on) Hibernate, they catch my own exceptions regarding DAO operations. Hence, I have code like this:

Code:
public class PapelListAction extends ActionSupport {
   // a lot of blah blah blah...

   public String delete() throws Exception {
      Papel papel = dao.get(id);
      
      if (papel == null)
         return ERROR;
      
      try {
         dao.delete(papel);
      } catch (CouldNotDeleteObjectException exception) {
         LOG.error(getText("usuario.falha-ao-enviar-senha-por-email"), exception);
         addActionError(getText("mensagens.impossivel-excluir-registro"));
      }
      
      return SUCCESS;
   }


Now suppose that a constraint violation exception is thrown during the object deletion. The CouldNotDeleteObjectException will never be thrown by the DAO because ConstraintViolationException is thrown only when Transaction.commit() is invoked, and the webapp's user will see an ugly stack trace. At least, that's what happens in my app. The code regarding session open-close span (and transaction management also) follows. Is it possible to catch constraint violation exceptions when Session.delete() is invoked?

Code:
public class HibernateTransactionManagerInterceptor implements Interceptor {
   private SessionFactory sessionFactory;
   
   public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
   }
   
   public String intercept(ActionInvocation actionInvocation) throws Exception {
      String result;
      Session session = sessionFactory.openSession();
      
      try {
         Transaction transaction = session.beginTransaction();
         
         try {
             TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
             TransactionSynchronizationManager.initSynchronization();
            
             try {
                result = actionInvocation.invoke();
               transaction.commit();
             } finally {
                 TransactionSynchronizationManager.unbindResource(sessionFactory);
                 TransactionSynchronizationManager.clearSynchronization();
             }
         } catch (Throwable throwable) {
            transaction.rollback();
            throw new Exception(throwable);
         }
      } finally {
         session.close();
      }
      
      return result;
   }
   
   public void init() {
   }
   
   public void destroy() {
   }
}


Hibernate version:

3.2

Full stack trace of any exception that occurs:

Code:
java.sql.SQLException: Integrity constraint violation USER_ROLE_ROLE_FK table: USER_ROLE in statement [delete from ROLE where ID=?]
   org.hsqldb.jdbc.Util.throwError(Unknown Source)
   org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
   com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
   org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
   org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2520)
   org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2697)
   org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:74)
   org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
   org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
   org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
   org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   br.com.radice.labore.web.HibernateTransactionManagerInterceptor.intercept(HibernateTransactionManagerInterceptor.java:34)
   com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
   org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:507)
   org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
   com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
   com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
   org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)


Name and version of the database you are using:

HSQLDB 1.8.0.7


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 5:39 pm 
Beginner
Beginner

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 27
We had a similar problem in which we needed to catch a unique constraint violation before the end of the transaction, but it wasn't getting posted until the commit was done. We found that by forcing a flush of the session we could get feedback more quickly. It might be worth a try in your case.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 7:36 am 
Newbie

Joined: Sat Nov 05, 2005 3:38 pm
Posts: 7
dhicks wrote:
We had a similar problem in which we needed to catch a unique constraint violation before the end of the transaction, but it wasn't getting posted until the commit was done. We found that by forcing a flush of the session we could get feedback more quickly. It might be worth a try in your case.


I tried that, but didn't work. :-(


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 10:11 am 
Newbie

Joined: Sat Nov 05, 2005 3:38 pm
Posts: 7
ccidral wrote:
I tried that, but didn't work. :-(


Now I'm getting a strange behaviour. I can't figure out whether this is either a hibernate or a struts2 issue. My code changed to this:

Code:
public class HibernateTransactionManagerInterceptor implements Interceptor {
   // other stuff ...

   public String intercept(ActionInvocation actionInvocation) throws Exception {
      String result;
      Session session = sessionFactory.openSession();
      
      // ADDED LINE
      session.setFlushMode(FlushMode.MANUAL);
      
      try {
         Transaction transaction = session.beginTransaction();
         
         try {
             TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
             TransactionSynchronizationManager.initSynchronization();
            
             try {
               result = actionInvocation.invoke();
            
               // ADDED LINE
               session.flush();

               transaction.commit();
             } finally {
                 TransactionSynchronizationManager.unbindResource(sessionFactory);
                 TransactionSynchronizationManager.clearSynchronization();
             }
         } catch (Throwable throwable) {
            transaction.rollback();
            throw new Exception(throwable);
         }
      } finally {
         session.close();
      }
      
      return result;
   }
}


Code:
public class BasicHibernateDao<T> implements BasicDao<T> {
   // other stuff ...
   
   public void delete(T object) {
      try {
         sessionFactory.getCurrentSession().delete(object);

         // ADDED LINE
         sessionFactory.getCurrentSession().flush();
      } catch (ConstraintViolationException cause) {
         throw new CouldNotDeleteObjectException(cause);
      }
   }
}


The stack trace now shows up as follows, where:

Code:
at br.com.radice.dao.hibernate.BasicHibernateDao.delete(BasicHibernateDao.java:42)

sessionFactory.getCurrentSession().flush();


Code:
at br.com.radice.labore.web.actions.PapelListAction.excluir(PapelListAction.java:63)

dao.delete(papel);


Code:
at br.com.radice.labore.web.HibernateTransactionManagerInterceptor.intercept(HibernateTransactionManagerInterceptor.java:36)

result = actionInvocation.invoke();


Code:
at br.com.radice.labore.web.HibernateTransactionManagerInterceptor.intercept(HibernateTransactionManagerInterceptor.java:38)

session.flush();


Code:
31/05/2007 09:09:06 org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: -8, SQLState: 23000
31/05/2007 09:09:06 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Integrity constraint violation USER_ROLE_ROLE_FK table: USER_ROLE in statement [delete from ROLE where ID=?]
31/05/2007 09:09:06 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [br.com.radice.labore.Papel#0a556b84f9a911db83140800200c9a66]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2541)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2697)
   at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:74)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at br.com.radice.dao.hibernate.BasicHibernateDao.delete(BasicHibernateDao.java:42)
   at br.com.radice.labore.web.actions.PapelListAction.excluir(PapelListAction.java:63)
   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:585)
   at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
   at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
   at br.com.radice.web.struts2.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:26)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
   at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
   at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at br.com.radice.labore.web.HibernateTransactionManagerInterceptor.intercept(HibernateTransactionManagerInterceptor.java:36)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
   at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:507)
   at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
   at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
   at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: Integrity constraint violation USER_ROLE_ROLE_FK table: USER_ROLE in statement [delete from ROLE where ID=?]
   at org.hsqldb.jdbc.Util.throwError(Unknown Source)
   at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2520)
   ... 141 more
31/05/2007 09:09:06 org.apache.struts2.components.URL start
WARNING: Unknown value for includeParams parameter to URL tag: false
31/05/2007 09:09:06 org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: -8, SQLState: 23000
31/05/2007 09:09:06 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Integrity constraint violation USER_ROLE_ROLE_FK table: USER_ROLE in statement [delete from ROLE where ID=?]
31/05/2007 09:09:06 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [br.com.radice.labore.Papel#0a556b84f9a911db83140800200c9a66]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2541)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2697)
   at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:74)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at br.com.radice.labore.web.HibernateTransactionManagerInterceptor.intercept(HibernateTransactionManagerInterceptor.java:38)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
   at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:507)
   at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
   at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
   at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: Integrity constraint violation USER_ROLE_ROLE_FK table: USER_ROLE in statement [delete from ROLE where ID=?]
   at org.hsqldb.jdbc.Util.throwError(Unknown Source)
   at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2520)
   ... 37 more
31/05/2007 09:09:06 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
java.sql.SQLException: Integrity constraint violation USER_ROLE_ROLE_FK table: USER_ROLE in statement [delete from ROLE where ID=?]
   at org.hsqldb.jdbc.Util.throwError(Unknown Source)
   at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2520)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2697)
   at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:74)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at br.com.radice.labore.web.HibernateTransactionManagerInterceptor.intercept(HibernateTransactionManagerInterceptor.java:38)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
   at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
   at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
   at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
   at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:507)
   at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
   at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
   at java.lang.Thread.run(Thread.java:595)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 11:31 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 27
Maybe you're already on top of this, but it's clear that you're trying to delete a ROLE object which is referred to by a USER_ROLE object, and you have a foreign key between the two. That'll throw an exception every time.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 8:03 am 
Newbie

Joined: Sat Nov 05, 2005 3:38 pm
Posts: 7
dhicks wrote:
Maybe you're already on top of this, but it's clear that you're trying to delete a ROLE object which is referred to by a USER_ROLE object, and you have a foreign key between the two. That'll throw an exception every time.


I know that. The problem is that the exception keeps being thrown at Transaction.commit() time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 10:18 am 
Newbie

Joined: Sat Nov 05, 2005 3:38 pm
Posts: 7
I will try to simplify my explanation. What I need is that exceptions regarding database constraint violations be thrown by Session.delete() and Session.save(), not by Transaction.commit(). Why? For architectural reasons I've explained before.

Is it possible? How? I have already tried a lot of things that didn't work. Also googled a lot but couldn't find any solution. May somebody help me, I would be grateful.

Thanks.

Code:
Session session = sessionFactory.openSession();

try {
    Transaction transaction = session.beginTransaction();
    try {
        session.delete(myObject); // <-- Any exception caused by constraint violation should be thrown from here ...
        transaction.commit(); // <-- ... and not from here.
    } catch (Throwable throwable) {
        transaction.rollback();
        throw new Exception(throwable);
    }
} finally {
    session.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 10:26 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
since hibernate is employing transactional write-behind, in most cases you won't get any exceptions resulting from database-constraints until commit-time which is when the session-state is written to the database.
If you want to have the exception thrown right after the delete()-call you'll have to call session.flush() right after session.delete() which will cause an immediate database-synchronisation.


Top
 Profile  
 
 Post subject: any solution?
PostPosted: Sun May 11, 2008 11:21 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
I know this is an old post, but is there a solution for this? I must catch this exception on the server side (EJB, CMT)...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.