Performance Zone is brought to you in partnership with:

I am a software architect passionate about software integration, high scalability and concurrency challenges. Vlad is a DZone MVB and is not an employee of DZone and has posted 43 posts at DZone. You can read more from them at their website. View Full User Profile

Optimistic locking auto retry with JPA

11.18.2013
| 7637 views |
  • submit to reddit

This is the third part of the optimistic locking series, and I will discuss how we can implement the automatic retry mechanism when dealing with JPA repositories. You can find the introductory part here and the MongoDB implementation here.

JPA requires running the persistence context code inside a Transaction, and if our Transaction Manager catches a RuntimeException, it initiates the rollback process. This makes the Persistence Context unusable, since we should discard it along with the roll-backed Transaction.

Therefore it's safer to retry the business logic operation when we are not within a running Transaction.

For this we altered our @Retry annotation as

public @interface Retry {

  Class<? extends Exception>[] on();

  int times() default 1;

  boolean failInTransaction() default true;
}

We here added the failInTransaction property which is set to true by default.

The Aspect was also changed to take into consideration the new annotation property.

private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable {
   int times = retryAnnotation.times();
   Class<? extends Throwable>[] retryOn = retryAnnotation.on();
   Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!");
   Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!");
   if (retryAnnotation.failInTransaction() && TransactionSynchronizationManager.isActualTransactionActive()) {
     throw new IllegalTransactionStateException(
         "You shouldn't retry an operation from withing an existing Transaction." +
         "This is because we can't retry if the current Transaction was already rollbacked!");
   }
   LOGGER.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn));
   return tryProceeding(pjp, times, retryOn);
}

If we are running inside a Transaction and we don't suppress the default safe-check we then throw an IllegalTransactionStateException, notifying the caller that's safer to retry if the Transaction is contained in the proceeding service call, without nesting our intercepting aspect.

Running a JUnit test which schedules 10 threads to update the same entity will generate optimistic locking exceptions and this is what the test outputs.

Line 102: INFO  [Thread-3]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class javax.persistence.OptimisticLockException]
Line 103: INFO  [Thread-12]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class javax.persistence.OptimisticLockException]
Line 104: INFO  [Thread-9]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class javax.persistence.OptimisticLockException]
Line 105: INFO  [Thread-6]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class javax.persistence.OptimisticLockException]
Line 109: INFO  [Thread-9]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class javax.persistence.OptimisticLockException]
Line 110: INFO  [Thread-7]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class javax.persistence.OptimisticLockException]
Line 114: INFO  [Thread-3]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class javax.persistence.OptimisticLockException]
Line 115: INFO  [Thread-5]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class javax.persistence.OptimisticLockException]
Line 117: INFO  [Thread-11]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class javax.persistence.OptimisticLockException]
Line 118: INFO  [Thread-6]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class javax.persistence.OptimisticLockException]
Line 123: INFO  [Thread-7]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class javax.persistence.OptimisticLockException]
Line 124: INFO  [Thread-5]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class javax.persistence.OptimisticLockException]
Line 126: INFO  [Thread-11]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class javax.persistence.OptimisticLockException]
Line 129: INFO  [Thread-5]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 7 remaining retries on [class javax.persistence.OptimisticLockException]

So we reused the same optimistic locking retry logic we first implemented for our MongoDB batch processors, proving we can easily implement such behavior even for JPA repositories.

Code available on GitHub.

Published at DZone with permission of Vlad Mihalcea, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)