title: How do I troubleshoot a "javax.persistence.RollbackException: Error while committing the transaction"?
body: <p>I’m using <code>Spring 3.1.4.RELEASE</code>, <code>Hibernate 4.1.0.Final</code>, <code>JPA 2</code>, <code>JBoss 7.1.3.AS</code> and <code>MySQL 5.5.37</code>.  Sometimes when we try and commit a transaction, it fails, although what is annoying is rolling back fails, so there is this lock in the system that never goes away and causes other things to fail.  The rollback exception stack trace is</p>
  
  <pre><code>05:00:07,751 ERROR [org.mainco.subco.myproject.service.OrganizationServiceImpl] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-4) Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
              at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) [spring-orm-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:387) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              at com.sun.proxy.$Proxy120.save(Unknown Source)
              at org.mainco.subco.myproject.service.OrganizationServiceImpl.updateAllOrgRecords(OrganizationServiceImpl.java:282) [pd-60.0.0.jar:]
              at org.mainco.subco.myproject.service.OrganizationServiceImpl.updateAllDistricts(OrganizationServiceImpl.java:219) [pd-60.0.0.jar:]
              at org.mainco.subco.myproject.service.OrganizationServiceImpl.updateOrgInfo(OrganizationServiceImpl.java:190) [pd-60.0.0.jar:]
              at org.mainco.subco.myproject.quartz.ProcessOrgsWorker.work(ProcessOrgsWorker.java:35) [classes:]
              at org.mainco.subco.myproject.quartz.ProcessOrgsJob.execute(ProcessOrgsJob.java:17) [classes:]
              at org.quartz.core.JobRunShell.run(JobRunShell.java:216) [quartz-1.8.5.jar:]
              at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) [quartz-1.8.5.jar:]
      Caused by: javax.persistence.RollbackException: Error while committing the transaction
              at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:90) [hibernate-entitymanager-4.1.0.Final.jar:4.1.0.Final]
              at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512) [spring-orm-3.1.4.RELEASE.jar:3.1.4.RELEASE]
              ... 14 more
      Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [org.mainco.subco.organization.domain.Organization] during persist time for groups [javax.validation.groups.Default, ]
  List of constraint violations:[
          ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=eodbId, rootBeanClass=class org.mainco.subco.organization.domain.Organization, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'}
  ]
              at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:184) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1127) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:325) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
              at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73) [hibernate-entitymanager-4.1.0.Final.jar:4.1.0.Final]
              ... 15 more
  </code></pre>
  
  <p>My question is how can I prevent RollBackExceptions?  Below is my configuration in Spring</p>
  
  <pre><code>    &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="packagesToScan" value="org.myproject.subco" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
      &lt;/property&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="jpaPropertyMap" ref="jpaPropertyMap" /&gt;
  &lt;/bean&gt;
  
      &lt;util:map id="jpaPropertyMap"&gt;
              &lt;entry key="hibernate.show_sql" value="false" /&gt;
              &lt;entry key="hibernate.hbm2ddl.auto" value="validate" /&gt;
              &lt;entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/&gt;
              &lt;entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" /&gt;
              &lt;entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/&gt;
              &lt;entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/&gt;
              &lt;entry key="hibernate.cache.use_second_level_cache" value="true" /&gt;
              &lt;entry key="hibernate.cache.use_query_cache" value="false" /&gt;
              &lt;entry key="hibernate.generate_statistics" value="false" /&gt;
              &lt;entry key="hibernate.connection.zeroDateTimeBehavior" value="convertToNull" /&gt;
      &lt;/util:map&gt;
  
      &lt;bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt;
              &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;jee:jndi-lookup jndi-name="java:jboss/datasources/MySqlDS" id="dataSource" expected-type="javax.sql.DataSource"/&gt;
  </code></pre>
  
  <p><strong>Note:</strong> my question is NOT how do I solve my transaction exceptions in general. I would like to build something flexible enough that if there is a commit problem, things can rollback normally.</p>