Skip to content

Commit b67cc42

Browse files
committed
fix for GRAILS-5823 "JAR Locking / Resource Clean Up"
1 parent 023209f commit b67cc42

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractSavePersistentMethod.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ public abstract class AbstractSavePersistentMethod extends AbstractDynamicPersis
6969
* the value. Note that this only works because the session is
7070
* flushed when a domain instance is saved without validation.
7171
*/
72-
private static ThreadLocal<Set<Integer>> disableAutoValidationFor = new ThreadLocal<Set<Integer>>() {
73-
@Override
74-
protected Set<Integer> initialValue() {
75-
return new HashSet<Integer>();
76-
}
77-
};
72+
private static ThreadLocal<Set<Integer>> disableAutoValidationFor = new ThreadLocal<Set<Integer>>();
7873

7974
static {
8075
ShutdownOperations.addOperation(new Runnable() {
@@ -84,17 +79,24 @@ public void run() {
8479
});
8580
}
8681

87-
public static boolean isAutoValidationDisabled(Object obj) {
82+
private static Set<Integer> getDisableAutoValidationFor() {
8883
Set<Integer> identifiers = disableAutoValidationFor.get();
84+
if (identifiers == null)
85+
disableAutoValidationFor.set(identifiers = new HashSet<Integer>());
86+
return identifiers;
87+
}
88+
89+
public static boolean isAutoValidationDisabled(Object obj) {
90+
Set<Integer> identifiers = getDisableAutoValidationFor();
8991
return obj != null && identifiers.contains(System.identityHashCode(obj));
9092
}
9193

9294
public static void clearDisabledValidations(Object obj) {
93-
disableAutoValidationFor.get().remove(System.identityHashCode(obj));
95+
getDisableAutoValidationFor().remove(System.identityHashCode(obj));
9496
}
9597

9698
public static void clearDisabledValidations() {
97-
disableAutoValidationFor.get().clear();
99+
getDisableAutoValidationFor().clear();
98100
}
99101

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

201203
if (!shouldValidate) {
202-
Set<Integer> identifiers = disableAutoValidationFor.get();
204+
Set<Integer> identifiers = getDisableAutoValidationFor();
203205
identifiers.add(System.identityHashCode(target));
204206
}
205207

grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernatePersistenceContextInterceptor.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,8 @@ public void run() {
4747
});
4848
}
4949

50-
private static ThreadLocal<Boolean> participate = new ThreadLocal<Boolean>() {
51-
@Override
52-
protected Boolean initialValue() {
53-
return Boolean.FALSE;
54-
}
55-
};
56-
57-
private static ThreadLocal<Integer> nestingCount = new ThreadLocal<Integer>() {
58-
@Override
59-
protected Integer initialValue() {
60-
return Integer.valueOf(0);
61-
}
62-
};
50+
private static ThreadLocal<Boolean> participate = new ThreadLocal<Boolean>();
51+
private static ThreadLocal<Integer> nestingCount = new ThreadLocal<Integer>();
6352

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

172161
private int incNestingCount() {
173-
int value = nestingCount.get().intValue() + 1;
174-
nestingCount.set(Integer.valueOf(value));
162+
Integer current = nestingCount.get();
163+
int value = (current != null) ? current + 1 : 1;
164+
nestingCount.set(value);
175165
return value;
176166
}
177167

178168
private int decNestingCount() {
179-
int value = nestingCount.get().intValue() - 1;
169+
Integer current = nestingCount.get();
170+
int value = (current != null) ? current - 1 : 0;
180171
if (value < 0) {
181172
value = 0;
182173
}
183-
nestingCount.set(Integer.valueOf(value));
174+
nestingCount.set(value);
184175
return value;
185176
}
186177

187178
private void setParticipate(boolean flag) {
188-
participate.set(Boolean.valueOf(flag));
179+
participate.set(flag);
189180
}
190181

191182
private boolean getParticipate() {
192-
return participate.get().booleanValue();
183+
Boolean ret = participate.get();
184+
return (ret != null) ? ret : false;
193185
}
194186
}

0 commit comments

Comments
 (0)