title: Spring Data JPA and MySQL: How to avoid "duplicate entry" error in multithreaded environment?
body: <p>My web application's database infrastructure is layered like this:</p>
  
  <ol>
  <li>Spring Data JPA</li>
  <li>JPA</li>
  <li>Hibernate</li>
  <li>C3P0 pool </li>
  <li>MySQL</li>
  </ol>
  
  <p>Some transactions take quite some time, sometimes one minute for one big transaction (the purpose is caching data into a database): an HTTP request from a user to my web server may begin this transaction. My web server then may query yet another remote third-party server for missing data. When all data is collected, the transaction finishes and all collected data is written to the database.</p>
  
  <p>It may happen that a user reloads my website during this long-running transaction. <strong>This causes to begin yet another transaction for the same data in a separate thread.</strong> As the purpose of this transaction is caching and therefore this operation is idempotent in nature, I don't mind having the same calculations going on twice. But in these situations, my web application sporadically runs into the following error. </p>
  
  <pre><code>20:12:46.392 container [Thread-24] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1062, SQLState: 23000
  20:12:46.392 container [Thread-24] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Duplicate entry '146845042025054' for key 'PRIMARY'
  20:12:46.602 container [Thread-24] INFO  o.h.e.j.b.internal.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements
  Exception in thread "Thread-24" org.springframework.dao.DataIntegrityViolationException: Duplicate entry '146845042025054' for key 'PRIMARY'; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Duplicate entry '146845042025054' for key 'PRIMARY'
      at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:643)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104)
      at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:516)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at com.sun.proxy.$Proxy45.save(Unknown Source)
      at com.mycompany.MyJpaEntityDomainClass.setId(MyJpaEntityDomainClass.java:83)
      at com.mycompany.MyCustomRepository.findMyJpaEntityDomainClassInstance(MyCustomRepository.java:72)
      at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at com.sun.proxy.$Proxy46.findMyJpaEntityDomainClassInstance(Unknown Source)
      at com.mycompany.AnotherDomainEntityClass.setAssociatedJpaEntityDomainInstances(AnotherDomainEntityClass.java:343)
      at com.mycompany.MyThreadInvokingService$SearcherThread.run(MyThreadInvokingService.java:106)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: org.hibernate.exception.ConstraintViolationException: Duplicate entry '146845042025054' for key 'PRIMARY'
      at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
      at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
      at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
      at com.sun.proxy.$Proxy76.executeUpdate(Unknown Source)
      at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2962)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3403)
      at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1210)
      at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:399)
      at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
      at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
      at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
      at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512)
      ... 31 more
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '146845042025054' for key 'PRIMARY'
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.Util.getInstance(Util.java:386)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355)
      at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
      ... 48 more
  </code></pre>
  
  <p>That call to <code>com.sun.proxy.$Proxy45.save</code> is actually a call to the <code>save</code> method of <code>MyJpaEntityDomainClassRepository extends org.springframework.data.jpa.repository.JpaRepository&lt;MyJpaEntityDomainClass, String&gt;</code>.</p>
  
  <p>As you can deduce from this stack trace, <code>MyJpaEntityDomainClass</code> instances take care of creating and updating themselves in the repository.</p>
  
  <p>I have the feeling that the <em>duplicate entry error</em> happens in situations of two concurrent threads trying to write the same entity to the database. I don't understand why this error happens at all, as the <code>org.springframework.data.jpa.repository.JpaRepositor.save( uniqueId )</code> in the slightly later thread should just overwrite the <code>uniqueId</code>-related entity/record put into the repository by the slightly earlier thread.</p>
  
  <p>How can I avoid such transaction-related duplicate entry race condition errors?</p>