From b67cc42727e9a6221923108b7df851c601396935 Mon Sep 17 00:00:00 2001 From: graemerocher Date: Mon, 21 May 2012 14:20:46 +0200 Subject: [PATCH] fix for GRAILS-5823 "JAR Locking / Resource Clean Up" --- .../AbstractSavePersistentMethod.java | 22 +++++++------- ...ibernatePersistenceContextInterceptor.java | 30 +++++++------------ 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractSavePersistentMethod.java b/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractSavePersistentMethod.java index a77fc9caba3..18993e88fb3 100644 --- a/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractSavePersistentMethod.java +++ b/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractSavePersistentMethod.java @@ -69,12 +69,7 @@ public abstract class AbstractSavePersistentMethod extends AbstractDynamicPersis * the value. Note that this only works because the session is * flushed when a domain instance is saved without validation. */ - private static ThreadLocal> disableAutoValidationFor = new ThreadLocal>() { - @Override - protected Set initialValue() { - return new HashSet(); - } - }; + private static ThreadLocal> disableAutoValidationFor = new ThreadLocal>(); static { ShutdownOperations.addOperation(new Runnable() { @@ -84,17 +79,24 @@ public void run() { }); } - public static boolean isAutoValidationDisabled(Object obj) { + private static Set getDisableAutoValidationFor() { Set identifiers = disableAutoValidationFor.get(); + if (identifiers == null) + disableAutoValidationFor.set(identifiers = new HashSet()); + return identifiers; + } + + public static boolean isAutoValidationDisabled(Object obj) { + Set identifiers = getDisableAutoValidationFor(); return obj != null && identifiers.contains(System.identityHashCode(obj)); } public static void clearDisabledValidations(Object obj) { - disableAutoValidationFor.get().remove(System.identityHashCode(obj)); + getDisableAutoValidationFor().remove(System.identityHashCode(obj)); } public static void clearDisabledValidations() { - disableAutoValidationFor.get().clear(); + getDisableAutoValidationFor().clear(); } public AbstractSavePersistentMethod(Pattern pattern, SessionFactory sessionFactory, @@ -199,7 +201,7 @@ protected Object doInvokeInternal(final Object target, Object[] arguments) { } if (!shouldValidate) { - Set identifiers = disableAutoValidationFor.get(); + Set identifiers = getDisableAutoValidationFor(); identifiers.add(System.identityHashCode(target)); } diff --git a/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernatePersistenceContextInterceptor.java b/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernatePersistenceContextInterceptor.java index 95597420f1d..ecaeea5e87b 100644 --- a/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernatePersistenceContextInterceptor.java +++ b/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernatePersistenceContextInterceptor.java @@ -47,19 +47,8 @@ public void run() { }); } - private static ThreadLocal participate = new ThreadLocal() { - @Override - protected Boolean initialValue() { - return Boolean.FALSE; - } - }; - - private static ThreadLocal nestingCount = new ThreadLocal() { - @Override - protected Integer initialValue() { - return Integer.valueOf(0); - } - }; + private static ThreadLocal participate = new ThreadLocal(); + private static ThreadLocal nestingCount = new ThreadLocal(); /* (non-Javadoc) * @see org.codehaus.groovy.grails.support.PersistenceContextInterceptor#destroy() @@ -170,25 +159,28 @@ public void setSessionFactory(SessionFactory sessionFactory) { } private int incNestingCount() { - int value = nestingCount.get().intValue() + 1; - nestingCount.set(Integer.valueOf(value)); + Integer current = nestingCount.get(); + int value = (current != null) ? current + 1 : 1; + nestingCount.set(value); return value; } private int decNestingCount() { - int value = nestingCount.get().intValue() - 1; + Integer current = nestingCount.get(); + int value = (current != null) ? current - 1 : 0; if (value < 0) { value = 0; } - nestingCount.set(Integer.valueOf(value)); + nestingCount.set(value); return value; } private void setParticipate(boolean flag) { - participate.set(Boolean.valueOf(flag)); + participate.set(flag); } private boolean getParticipate() { - return participate.get().booleanValue(); + Boolean ret = participate.get(); + return (ret != null) ? ret : false; } }