@RetryTransaction:如何在发现死锁时使其工作

时间:2022-06-05 20:33:56

Concurent Transaction make my Sql statements fail. I'm trying to use [this] dellroad-stuff1. But it seems to be ignored. I m working with spring 3 and hibernate 4.

Concurent Transaction使我的Sql语句失败。我正在尝试使用[this] dellroad-stuff1。但它似乎被忽略了。我正在使用spring 3和hibernate 4。

The error :

错误 :

15:32:11,331 WARN SqlExceptionHelper:145 - SQL Error: 1213, SQLState: 40001
15:32:11,331 ERROR SqlExceptionHelper:147 - Deadlock found when trying to get lock; try restarting transaction
15:32:11,334 INFO AbstractBatchImpl:195 - HHH000010: On release of batch it still contained JDBC statements

The annoted Function to retry transaction if failed :

如果失败,则重写事务的annoted函数:

@Override   
@RetryTransaction   
@Transactional  
public void save(AnalyseResult analyseResult) {
    final int attempt = RetryTransactionAspect.aspectOf().getAttemptNumber();
    System.out.println("#############");
    System.out.println("Retry Transact : "+attempt);
    System.out.println("#############"); 
    analyseResultDao.save(analyseResult);
}

The Beans.xml

Beans.xml

<!--  An @AspectJ aspect will be interpreted as an aspect by Spring AOP and beans
      in the context will be advised accordingly -->
<aop:aspectj-autoproxy proxy-target-class="true" />

<bean id="hibernateExceptionTranslator" class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/> 

<bean class="org.dellroad.stuff.spring.RetryTransactionAspect" factory-method="aspectOf">
     <property name="persistenceExceptionTranslator" ref="hibernateExceptionTranslator"></property>
     <property name="maxRetriesDefault" value="4"></property>
     <property name="initialDelayDefault" value="25"></property>
     <property name="maximumDelayDefault" value="5000"></property>
</bean>

1 个解决方案

#1


0  

Check you've satisfied all of the following (per the Javadoc):

检查您是否满足以下所有条件(根据Javadoc):

  • The method (and/or the containing type) must be annotated with both @Transactional and @RetryTransaction
  • 必须使用@Transactional和@RetryTransaction注释方法(和/或包含类型)
  • The @Transactional annotation must have propagation set to either PROPAGATION_REQUIRED or PROPAGATION_REQUIRES_NEW (other propagation values do not involve creating new transactions).
  • @Transactional注释必须将传播设置为PROPAGATION_REQUIRED或PROPAGATION_REQUIRES_NEW(其他传播值不涉及创建新事务)。
  • In the case of PROPAGATION_REQUIRED propagation, there must not be a transaction already open in the calling thread (under the same transaction manager). In other words, the invoked method must be the one responsible for creating a new transaction.
  • 在PROPAGATION_REQUIRED传播的情况下,调用线程(在同一事务管理器下)不得打开事务。换句话说,调用的方法必须是负责创建新事务的方法。
  • The method's class must be woven (either at build time or runtime) using the AspectJ compiler with the RetryTransactionAspect aspect (included in the dellroad-stuff JAR file).
  • 必须使用带有RetryTransactionAspect方面的AspectJ编译器(包含在dellroad-stuff JAR文件中)编译方法的类(在构建时或运行时)。
  • The RetryTransactionAspect aspect must be configured with a PersistenceExceptionTranslator appropriate for the ORM layer being used. The simplest way to do this is to include the aspect in your Spring application context, for example:

    必须使用适合所使用的ORM层的PersistenceExceptionTranslator配置RetryTransactionAspect方面。最简单的方法是在Spring应用程序上下文中包含该方面,例如:

    <bean class="org.dellroad.stuff.spring.RetryTransactionAspect" factory-method="aspectOf" p:persistenceExceptionTranslator-ref="myJpaDialect"/>

    This also gives you the opportunity to change the default values for maxRetries(), initialDelay(), and maximumDelay(), which are applied when not explicitly overridden in the annotation, for example:

    这也使您有机会更改maxRetries(),initialDelay()和maximumDelay()的默认值,这些默认值在未在注释中显式覆盖时应用,例如:

    <bean class="org.dellroad.stuff.spring.RetryTransactionAspect" factory-method="aspectOf" p:persistenceExceptionTranslator-ref="myJpaDialect" p:maxRetriesDefault="2" p:initialDelayDefault="25" p:maximumDelayDefault="5000"/>

#1


0  

Check you've satisfied all of the following (per the Javadoc):

检查您是否满足以下所有条件(根据Javadoc):

  • The method (and/or the containing type) must be annotated with both @Transactional and @RetryTransaction
  • 必须使用@Transactional和@RetryTransaction注释方法(和/或包含类型)
  • The @Transactional annotation must have propagation set to either PROPAGATION_REQUIRED or PROPAGATION_REQUIRES_NEW (other propagation values do not involve creating new transactions).
  • @Transactional注释必须将传播设置为PROPAGATION_REQUIRED或PROPAGATION_REQUIRES_NEW(其他传播值不涉及创建新事务)。
  • In the case of PROPAGATION_REQUIRED propagation, there must not be a transaction already open in the calling thread (under the same transaction manager). In other words, the invoked method must be the one responsible for creating a new transaction.
  • 在PROPAGATION_REQUIRED传播的情况下,调用线程(在同一事务管理器下)不得打开事务。换句话说,调用的方法必须是负责创建新事务的方法。
  • The method's class must be woven (either at build time or runtime) using the AspectJ compiler with the RetryTransactionAspect aspect (included in the dellroad-stuff JAR file).
  • 必须使用带有RetryTransactionAspect方面的AspectJ编译器(包含在dellroad-stuff JAR文件中)编译方法的类(在构建时或运行时)。
  • The RetryTransactionAspect aspect must be configured with a PersistenceExceptionTranslator appropriate for the ORM layer being used. The simplest way to do this is to include the aspect in your Spring application context, for example:

    必须使用适合所使用的ORM层的PersistenceExceptionTranslator配置RetryTransactionAspect方面。最简单的方法是在Spring应用程序上下文中包含该方面,例如:

    <bean class="org.dellroad.stuff.spring.RetryTransactionAspect" factory-method="aspectOf" p:persistenceExceptionTranslator-ref="myJpaDialect"/>

    This also gives you the opportunity to change the default values for maxRetries(), initialDelay(), and maximumDelay(), which are applied when not explicitly overridden in the annotation, for example:

    这也使您有机会更改maxRetries(),initialDelay()和maximumDelay()的默认值,这些默认值在未在注释中显式覆盖时应用,例如:

    <bean class="org.dellroad.stuff.spring.RetryTransactionAspect" factory-method="aspectOf" p:persistenceExceptionTranslator-ref="myJpaDialect" p:maxRetriesDefault="2" p:initialDelayDefault="25" p:maximumDelayDefault="5000"/>