From 2c3be8f37ed750ccb0e97db9a7994cf018c4ece4 Mon Sep 17 00:00:00 2001 From: dsyer Date: Mon, 6 Sep 2010 12:42:05 +0000 Subject: [PATCH] Add synchronized block to prevent intermittent test failures --- .../TransactionAwareProxyFactory.java | 94 ++++++++++--------- 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareProxyFactory.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareProxyFactory.java index ae31900dd..eaeabc61b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareProxyFactory.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareProxyFactory.java @@ -141,52 +141,16 @@ public class TransactionAwareProxyFactory { private T createInstance() { - ProxyFactory factory = new ProxyFactory(target); - factory.addAdvice(new MethodInterceptor() { + synchronized (target) { - public Object invoke(MethodInvocation invocation) throws Throwable { + ProxyFactory factory = new ProxyFactory(target); + factory.addAdvice(new TransactionAwareInterceptor()); + @SuppressWarnings("unchecked") + T instance = (T) factory.getProxy(); + return instance; - if (!TransactionSynchronizationManager.isActualTransactionActive()) { - return invocation.proceed(); - } + } - T cache; - - if (!TransactionSynchronizationManager.hasResource(this)) { - cache = begin(target); - TransactionSynchronizationManager.bindResource(this, cache); - TransactionSynchronizationManager.registerSynchronization(new TargetSynchronization(this, cache)); - } - else { - @SuppressWarnings("unchecked") - T retrievedCache = (T) TransactionSynchronizationManager.getResource(this); - cache = retrievedCache; - } - - Object result = invocation.getMethod().invoke(cache, invocation.getArguments()); - - if (appendOnly) { - String methodName = invocation.getMethod().getName(); - if ((result == null && methodName.equals("get")) - || (Boolean.FALSE.equals(result) && (methodName.startsWith("contains")) || (Boolean.TRUE.equals(result) && methodName.startsWith("isEmpty")))) { - // In appendOnly mode the result of a get might not be - // in the cache... - return invocation.proceed(); - } - if (result instanceof Collection) { - HashSet set = new HashSet((Collection) result); - set.addAll((Collection) invocation.proceed()); - result = set; - } - } - - return result; - - } - }); - @SuppressWarnings("unchecked") - T instance = (T) factory.getProxy(); - return instance; } @SuppressWarnings("unchecked") @@ -257,4 +221,48 @@ public class TransactionAwareProxyFactory { } } + private class TransactionAwareInterceptor implements MethodInterceptor { + + public Object invoke(MethodInvocation invocation) throws Throwable { + + if (!TransactionSynchronizationManager.isActualTransactionActive()) { + return invocation.proceed(); + } + + T cache; + + if (!TransactionSynchronizationManager.hasResource(this)) { + cache = begin(target); + TransactionSynchronizationManager.bindResource(this, cache); + TransactionSynchronizationManager.registerSynchronization(new TargetSynchronization(this, cache)); + } + else { + @SuppressWarnings("unchecked") + T retrievedCache = (T) TransactionSynchronizationManager.getResource(this); + cache = retrievedCache; + } + + Object result = invocation.getMethod().invoke(cache, invocation.getArguments()); + + if (appendOnly) { + String methodName = invocation.getMethod().getName(); + if ((result == null && methodName.equals("get")) + || (Boolean.FALSE.equals(result) && (methodName.startsWith("contains")) || (Boolean.TRUE + .equals(result) && methodName.startsWith("isEmpty")))) { + // In appendOnly mode the result of a get might not be + // in the cache... + return invocation.proceed(); + } + if (result instanceof Collection) { + HashSet set = new HashSet((Collection) result); + set.addAll((Collection) invocation.proceed()); + result = set; + } + } + + return result; + + } + } + }