Skip to content

Commit

Permalink
fix for GRAILS-5823 "JAR Locking / Resource Clean Up"
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed May 21, 2012
1 parent 023209f commit b67cc42
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
Expand Up @@ -69,12 +69,7 @@ public abstract class AbstractSavePersistentMethod extends AbstractDynamicPersis
* the value. Note that this only works because the session is * the value. Note that this only works because the session is
* flushed when a domain instance is saved without validation. * flushed when a domain instance is saved without validation.
*/ */
private static ThreadLocal<Set<Integer>> disableAutoValidationFor = new ThreadLocal<Set<Integer>>() { private static ThreadLocal<Set<Integer>> disableAutoValidationFor = new ThreadLocal<Set<Integer>>();
@Override
protected Set<Integer> initialValue() {
return new HashSet<Integer>();
}
};


static { static {
ShutdownOperations.addOperation(new Runnable() { ShutdownOperations.addOperation(new Runnable() {
Expand All @@ -84,17 +79,24 @@ public void run() {
}); });
} }


public static boolean isAutoValidationDisabled(Object obj) { private static Set<Integer> getDisableAutoValidationFor() {
Set<Integer> identifiers = disableAutoValidationFor.get(); Set<Integer> identifiers = disableAutoValidationFor.get();
if (identifiers == null)
disableAutoValidationFor.set(identifiers = new HashSet<Integer>());
return identifiers;
}

public static boolean isAutoValidationDisabled(Object obj) {
Set<Integer> identifiers = getDisableAutoValidationFor();
return obj != null && identifiers.contains(System.identityHashCode(obj)); return obj != null && identifiers.contains(System.identityHashCode(obj));
} }


public static void clearDisabledValidations(Object obj) { public static void clearDisabledValidations(Object obj) {
disableAutoValidationFor.get().remove(System.identityHashCode(obj)); getDisableAutoValidationFor().remove(System.identityHashCode(obj));
} }


public static void clearDisabledValidations() { public static void clearDisabledValidations() {
disableAutoValidationFor.get().clear(); getDisableAutoValidationFor().clear();
} }


public AbstractSavePersistentMethod(Pattern pattern, SessionFactory sessionFactory, public AbstractSavePersistentMethod(Pattern pattern, SessionFactory sessionFactory,
Expand Down Expand Up @@ -199,7 +201,7 @@ protected Object doInvokeInternal(final Object target, Object[] arguments) {
} }


if (!shouldValidate) { if (!shouldValidate) {
Set<Integer> identifiers = disableAutoValidationFor.get(); Set<Integer> identifiers = getDisableAutoValidationFor();
identifiers.add(System.identityHashCode(target)); identifiers.add(System.identityHashCode(target));
} }


Expand Down
Expand Up @@ -47,19 +47,8 @@ public void run() {
}); });
} }


private static ThreadLocal<Boolean> participate = new ThreadLocal<Boolean>() { private static ThreadLocal<Boolean> participate = new ThreadLocal<Boolean>();
@Override private static ThreadLocal<Integer> nestingCount = new ThreadLocal<Integer>();
protected Boolean initialValue() {
return Boolean.FALSE;
}
};

private static ThreadLocal<Integer> nestingCount = new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return Integer.valueOf(0);
}
};


/* (non-Javadoc) /* (non-Javadoc)
* @see org.codehaus.groovy.grails.support.PersistenceContextInterceptor#destroy() * @see org.codehaus.groovy.grails.support.PersistenceContextInterceptor#destroy()
Expand Down Expand Up @@ -170,25 +159,28 @@ public void setSessionFactory(SessionFactory sessionFactory) {
} }


private int incNestingCount() { private int incNestingCount() {
int value = nestingCount.get().intValue() + 1; Integer current = nestingCount.get();
nestingCount.set(Integer.valueOf(value)); int value = (current != null) ? current + 1 : 1;
nestingCount.set(value);
return value; return value;
} }


private int decNestingCount() { private int decNestingCount() {
int value = nestingCount.get().intValue() - 1; Integer current = nestingCount.get();
int value = (current != null) ? current - 1 : 0;
if (value < 0) { if (value < 0) {
value = 0; value = 0;
} }
nestingCount.set(Integer.valueOf(value)); nestingCount.set(value);
return value; return value;
} }


private void setParticipate(boolean flag) { private void setParticipate(boolean flag) {
participate.set(Boolean.valueOf(flag)); participate.set(flag);
} }


private boolean getParticipate() { private boolean getParticipate() {
return participate.get().booleanValue(); Boolean ret = participate.get();
return (ret != null) ? ret : false;
} }
} }

0 comments on commit b67cc42

Please sign in to comment.