Spring Data JPA和MySQL:如何在多线程环境中避免“重复输入”错误?

时间:2013-06-18 21:36:18

标签: mysql hibernate jpa c3p0 spring-data-jpa

我的Web应用程序的数据库基础结构是这样分层的:

  1. Spring Data JPA
  2. JPA
  3. 休眠
  4. C3P0池
  5. MySQL的
  6. 某些事务需要相当长的时间,有时一分钟用于一个大事务(目的是将数据缓存到数据库中):从用户到我的Web服务器的HTTP请求可能会开始此事务。然后,我的Web服务器可以向另一个远程第三方服务器查询丢失的数据。收集完所有数据后,事务就会完成,所有收集的数据都会写入数据库。

    在这个长时间运行的交易中,用户可能会重新加载我的网站。 这导致在单独的线程中为相同的数据开始另一个事务。由于此事务的目的是缓存,因此这个操作本质上是幂等的,我不介意进行相同的计算两次。但在这些情况下,我的Web应用程序偶尔会遇到以下错误。

    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
    

    com.sun.proxy.$Proxy45.save的来电实际上是对save的{​​{1}}方法的调用。

    正如您可以从此堆栈跟踪中推断出的那样,MyJpaEntityDomainClassRepository extends org.springframework.data.jpa.repository.JpaRepository<MyJpaEntityDomainClass, String>实例负责在存储库中创建和更新自己。

    我感觉重复条目错误在两个并发线程试图将同一个实体写入数据库的情况下发生。我不明白为什么会发生这种错误,因为稍后的线程中的MyJpaEntityDomainClass应该只是覆盖稍早一点的线程放入存储库的org.springframework.data.jpa.repository.JpaRepositor.save( uniqueId )相关实体/记录。

    如何避免此类与交易相关的重复输入竞争条件错误?

2 个答案:

答案 0 :(得分:1)

尝试插入同一实体的两个记录时,会发生重复输入错误。 要检查的事情是:

  • 是执行相同操作的线程共享的JPA / Hibernate会话 在错误发生时更新。
  • 如何生成唯一记录ID? (myqsl 自动递增 ; id生成器代码。等)如果有一个id 列与实际主键(约束)不同 表格。

答案 1 :(得分:0)

从你的帖子我可以告诉你正在使用事务,所以你必须以某种方式同步线程。最简单但不是最便携或最有效的解决方案是锁定您可能正在使用@Transactional注释的表,您应该将事务的隔离添加为Isolation.SERIALIZABLE

@Transactional(isolation = Isolation.SERIALIZABLE)

如果您的应用程序在单个JVM上运行,另一种简单的方法是使一个方法只能访问一个方法,那就是使用synchronized关键字。

 public synchronized  void method(){}

您可以创建共享数据结构并将其保存在内存中,并像方法一样同步或使用像hazelcast或terracotta this answer goes deeper in this approach这样的框架。

我能想到的最后一种方法是最可靠,最有效和最困难的方法。那就是通过消息JMS等同步你的应用程序。

问题可能还在于条目已经存在,因此您可以使用以下内容查询最新的ID:

Entity findTop1ByOrderByIdDesc();

然后恢复id并添加一个如果你删除你的entites并且不是最有效但可以完成任务就可以留下漏洞。

另请参阅diarmuid提到的第二个建议。实体的JPA定义中主键的startegy。

@Id
@GeneratedValue(strategy = GenerationType.AUTO) //This one
@Column(name = "id", nullable = false)
public int getId() {
    return id;
}

并仔细检查表定义中id是否有自动增量。作为最后一个建议,您还可以检查您的事务是否访问了多个实体,其中一个实体可能缺少GeneratedValue,这可能是发送消息的罪魁祸首。