title: Spring - Unable to catch ConstraintViolationException when deleting an entity referenced in another table
body: <p>I am trying to improve the way i handle exceptions coming from my DAO / service layers in my spring MVC app.  The method i use below works very nicely and does not cause a 500 error :</p>
  
  <p>Controller code</p>
  
  <pre><code>@RequestMapping(value = {"/delete-{id}-strengthUnit"}, method = RequestMethod.GET)
  public String deleteStrengthUnit(@PathVariable String id) 
  {
      StrengthUnit strengthUnit = strengthUnitService.findById(Integer.parseInt(id));
  
      try
      {
          strengthUnitService.delete(strengthUnit);
  
          session.setAttribute("successMessage", "Successfully deleted strength unit \"" + strengthUnit.getName() + "\"!");
      }
      catch (Exception ex)
      {
          session.setAttribute("errorMessage", "Strength unit \"" + strengthUnit.getName() + "\" is in use and cannot be deleted!");
      }
  
      return "redirect:/strengthUnits/list";
  }
  </code></pre>
  
  <p>Service code </p>
  
  <pre><code>@Override
  public void delete(StrengthUnit strengthUnit) throws ConstraintViolationException
  {
      dao.delete(strengthUnit);
  }
  </code></pre>
  
  <p>DAO code</p>
  
  <pre><code>@Override
  public void delete(StrengthUnit strengthUnit) throws ConstraintViolationException
  {
      super.delete(strengthUnit);
  }
  </code></pre>
  
  <p>Master DAO code (all DAOs extend this abstract DAO class)</p>
  
  <pre><code>public void delete(T entity)
  {
      getSession().delete(entity);
  }
  </code></pre>
  
  <p>This code chain correctly intercepts the ConstraintViolationException (thrown by the DAO layer and forwarded to the controller by the service layer) and sets a variable in the session object with a nice message to present to the user in the next page.  All this works perfectly as it is now.  </p>
  
  <p>I am trying to improve my service layer so it can do all the exception catching and logging duties which result from interacting with the DAO layer.  I am trying to make this very simple change and i cannot for the life of me figure out why it is unable to catch the ConstraintViolationException when doing it as below :</p>
  
  <p>Controller code </p>
  
  <pre><code>@RequestMapping(value = {"/delete-{id}-strengthUnit"}, method = RequestMethod.GET)
  public String deleteStrengthUnit(@PathVariable String id) 
  {
      StrengthUnit strengthUnit = strengthUnitService.findById(Integer.parseInt(id));
  
      try
      {
          strengthUnitService.delete(strengthUnit);
  
          session.setAttribute("successMessage", "Successfully deleted strength unit \"" + strengthUnit.getName() + "\"!");
      }
      catch (EntityInUseException ex)
      {
          session.setAttribute("errorMessage", "Strength unit \"" + strengthUnit.getName() + "\" is in use and cannot be deleted!");
      }
  
      return "redirect:/strengthUnits/list";
  }
  </code></pre>
  
  <p>Service code</p>
  
  <pre><code>@Override
  public void delete(StrengthUnit strengthUnit) throws EntityInUseException
  {
      try
      {
          dao.delete(strengthUnit);
  
          logEntryService.logDebug(LogEntry.SHARED, LogEntry.DATABASE, "Successfully deleted strength unit \"" + strengthUnit.getName() + "\"", logger);
      }
      catch (Exception ex)
      {
          logEntryService.logError(LogEntry.SHARED, LogEntry.DATABASE, "Error deleting strength unit \"" + strengthUnit.getName() + "\" : " + ex.getMessage(), logger);
  
          throw new EntityInUseException("Strength unit \"" + strengthUnit.getName() + "\" is in use and cannot be deleted!");
      }
  }
  </code></pre>
  
  <p>The DAO code is completely unchanged.  With these simple changes, the controller should receive my custom exception but it does not.  The execution does not enter the catch block in the service layer even though things were working perfectly fine before the changes with no changes to the DAO layer.  Here is the exception i get when the code completely skips the catch block :</p>
  
  <pre><code>java.sql.BatchUpdateException: The DELETE statement conflicted with the REFERENCE constraint "FKlylli8qlwprfk1t59bx6krbk3". The conflict occurred in database "MDHIS", table "dbo.tblMed", column 'id_strenghUnit'.
  at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:1069)
  at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:2544)
  at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:118)
  at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:104)
  at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.execute(AbstractBatchImpl.java:147)
  at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.executeBatch(JdbcCoordinatorImpl.java:212)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:633)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
  at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
  at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
  at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
  at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511)
  at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3278)
  at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2474)
  at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:39)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271)
  at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98)
  at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:609)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:532)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
  at com.sun.proxy.$Proxy107.delete(Unknown Source)
  at com.mdenis.mdhis_webclient.controller.StrengthUnitController.deleteStrengthUnit(StrengthUnitController.java:155)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at com.mdenis.mdhis_webclient.filter.PermissionsFilter.doFilter(PermissionsFilter.java:125)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at com.mdenis.mdhis_webclient.filter.AuthenticationFilter.doFilter(AuthenticationFilter.java:75)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
  at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
  at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
  at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
  at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
  at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>I looked at another entity's delete operation (one that i didn't change yet) and the Exception is actually a DataIntegrityViolationException.  Even when setting my DAO to throw that exception, i am unable to have my service layer catch it.  The catch block is never reached and it prints a success message in the logs even though i get the 500 error and the above stacktrace immediately after.</p>
  
  <p>Does this make sense to anyone?</p>
  
  <p>Thanks!</p>