@RetryTransaction: how to make it work when a dead end is found

A consistent transaction will cause my Sql statements to fail. I am trying to use [this] dellroad-stuff 1 . But it seems to be ignored. I work with spring 3 and sleeping 4.

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 

Annotated function for re-transaction if failed:

 @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); } 

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> 
+5
source share
1 answer

Make sure that you have met all of the following requirements (according to Javadoc ):

  • The method (and / or containing type) must be annotated as @Transactional and @RetryTransaction
  • @Transactional annotations must have a distribution set of either PROPAGATION_REQUIRED or PROPAGATION_REQUIRES_NEW (other distribution values ​​are not related to creating new transactions).
  • In the case of the distribution of PROPAGATION_REQUIRED there should not be a transaction already open in the calling thread (under the same transaction manager). In other words, the called method must be responsible for creating a new transaction.
  • The method class must be gossiped (either at build time or at run time) using the AspectJ compiler with the RetryTransactionAspect aspect (included in the dellroad-stuff JAR file).
  • RetryTransactionAspect must be configured with the PersistenceExceptionTranslator appropriate for the ORM level used. The easiest way to do this is to include an aspect in the context of the Spring application, for example:

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

    It also gives you the ability to change the default values ​​for maxRetries (), initialDelay (), and MaximumDelay (), which apply if they were not explicitly overridden in the annotation, for example:

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

Source: https://habr.com/ru/post/1208798/


All Articles