@@ -155,6 +155,10 @@ private static Object doGetResource(Object actualKey) {
155
155
// Transparently remove ResourceHolder that was marked as void...
156
156
if (value instanceof ResourceHolder && ((ResourceHolder ) value ).isVoid ()) {
157
157
map .remove (actualKey );
158
+ // Remove entire ThreadLocal if empty...
159
+ if (map .isEmpty ()) {
160
+ resources .remove ();
161
+ }
158
162
value = null ;
159
163
}
160
164
return value ;
@@ -176,8 +180,13 @@ public static void bindResource(Object key, Object value) throws IllegalStateExc
176
180
map = new HashMap <Object , Object >();
177
181
resources .set (map );
178
182
}
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 [" +
181
190
actualKey + "] bound to thread [" + Thread .currentThread ().getName () + "]" );
182
191
}
183
192
if (logger .isTraceEnabled ()) {
@@ -226,6 +235,10 @@ private static Object doUnbindResource(Object actualKey) {
226
235
if (map .isEmpty ()) {
227
236
resources .remove ();
228
237
}
238
+ // Transparently suppress a ResourceHolder that was marked as void...
239
+ if (value instanceof ResourceHolder && ((ResourceHolder ) value ).isVoid ()) {
240
+ value = null ;
241
+ }
229
242
if (value != null && logger .isTraceEnabled ()) {
230
243
logger .trace ("Removed value [" + value + "] for key [" + actualKey + "] from thread [" +
231
244
Thread .currentThread ().getName () + "]" );
0 commit comments