Skip to content

Commit c9b36fb

Browse files
committed
TransactionSynchronizationManager eagerly cleans up void ResourceHolders on any access (SPR-8844, SPR-8845)
1 parent a9a068e commit c9b36fb

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ private static Object doGetResource(Object actualKey) {
155155
// Transparently remove ResourceHolder that was marked as void...
156156
if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
157157
map.remove(actualKey);
158+
// Remove entire ThreadLocal if empty...
159+
if (map.isEmpty()) {
160+
resources.remove();
161+
}
158162
value = null;
159163
}
160164
return value;
@@ -176,8 +180,13 @@ public static void bindResource(Object key, Object value) throws IllegalStateExc
176180
map = new HashMap<Object, Object>();
177181
resources.set(map);
178182
}
179-
if (map.put(actualKey, value) != null) {
180-
throw new IllegalStateException("Already value [" + map.get(actualKey) + "] for key [" +
183+
Object oldValue = map.put(actualKey, value);
184+
// Transparently suppress a ResourceHolder that was marked as void...
185+
if (oldValue instanceof ResourceHolder && ((ResourceHolder) oldValue).isVoid()) {
186+
oldValue = null;
187+
}
188+
if (oldValue != null) {
189+
throw new IllegalStateException("Already value [" + oldValue + "] for key [" +
181190
actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
182191
}
183192
if (logger.isTraceEnabled()) {
@@ -226,6 +235,10 @@ private static Object doUnbindResource(Object actualKey) {
226235
if (map.isEmpty()) {
227236
resources.remove();
228237
}
238+
// Transparently suppress a ResourceHolder that was marked as void...
239+
if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
240+
value = null;
241+
}
229242
if (value != null && logger.isTraceEnabled()) {
230243
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from thread [" +
231244
Thread.currentThread().getName() + "]");

0 commit comments

Comments
 (0)