Skip to content

Commit e56cfb8

Browse files
committed
consistent use of JDK 1.5's ThreadLocal.remove() over ThreadLocal.set(null), preventing leaks (SPR-7441)
1 parent 66a5bb7 commit e56cfb8

14 files changed

Lines changed: 47 additions & 42 deletions

File tree

org.springframework.aop/src/main/java/org/springframework/aop/framework/AopContext.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,7 +76,12 @@ public static Object currentProxy() throws IllegalStateException {
7676
*/
7777
static Object setCurrentProxy(Object proxy) {
7878
Object old = currentProxy.get();
79-
currentProxy.set(proxy);
79+
if (proxy != null) {
80+
currentProxy.set(proxy);
81+
}
82+
else {
83+
currentProxy.remove();
84+
}
8085
return old;
8186
}
8287

org.springframework.aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -106,7 +106,7 @@ public void destroy() {
106106
this.targetSet.clear();
107107
}
108108
// Clear ThreadLocal, just in case.
109-
this.targetInThread.set(null);
109+
this.targetInThread.remove();
110110
}
111111

112112

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,13 @@ else if (curVal instanceof String) {
904904
protected void afterPrototypeCreation(String beanName) {
905905
Object curVal = this.prototypesCurrentlyInCreation.get();
906906
if (curVal instanceof String) {
907-
this.prototypesCurrentlyInCreation.set(null);
907+
this.prototypesCurrentlyInCreation.remove();
908908
}
909909
else if (curVal instanceof Set) {
910910
Set<String> beanNameSet = (Set<String>) curVal;
911911
beanNameSet.remove(beanName);
912912
if (beanNameSet.isEmpty()) {
913-
this.prototypesCurrentlyInCreation.set(null);
913+
this.prototypesCurrentlyInCreation.remove();
914914
}
915915
}
916916
}

org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefin
344344
finally {
345345
currentResources.remove(encodedResource);
346346
if (currentResources.isEmpty()) {
347-
this.resourcesCurrentlyBeingLoaded.set(null);
347+
this.resourcesCurrentlyBeingLoaded.remove();
348348
}
349349
}
350350
}

org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -496,16 +496,16 @@ public void afterPropertiesSet() throws Exception {
496496

497497
finally {
498498
if (this.resourceLoader != null) {
499-
configTimeResourceLoaderHolder.set(null);
499+
configTimeResourceLoaderHolder.remove();
500500
}
501501
if (this.taskExecutor != null) {
502-
configTimeTaskExecutorHolder.set(null);
502+
configTimeTaskExecutorHolder.remove();
503503
}
504504
if (this.dataSource != null) {
505-
configTimeDataSourceHolder.set(null);
505+
configTimeDataSourceHolder.remove();
506506
}
507507
if (this.nonTransactionalDataSource != null) {
508-
configTimeNonTransactionalDataSourceHolder.set(null);
508+
configTimeNonTransactionalDataSourceHolder.remove();
509509
}
510510
}
511511

org.springframework.context/src/main/java/org/springframework/context/i18n/LocaleContextHolder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,8 +51,8 @@ public abstract class LocaleContextHolder {
5151
* Reset the LocaleContext for the current thread.
5252
*/
5353
public static void resetLocaleContext() {
54-
localeContextHolder.set(null);
55-
inheritableLocaleContextHolder.set(null);
54+
localeContextHolder.remove();
55+
inheritableLocaleContextHolder.remove();
5656
}
5757

5858
/**
@@ -75,11 +75,11 @@ public static void setLocaleContext(LocaleContext localeContext) {
7575
public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) {
7676
if (inheritable) {
7777
inheritableLocaleContextHolder.set(localeContext);
78-
localeContextHolder.set(null);
78+
localeContextHolder.remove();
7979
}
8080
else {
8181
localeContextHolder.set(localeContext);
82-
inheritableLocaleContextHolder.set(null);
82+
inheritableLocaleContextHolder.remove();
8383
}
8484
}
8585

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/UserCredentialsDataSourceAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -114,7 +114,7 @@ public void setCredentialsForCurrentThread(String username, String password) {
114114
* @see #setCredentialsForCurrentThread
115115
*/
116116
public void removeCredentialsFromCurrentThread() {
117-
this.threadBoundCredentials.set(null);
117+
this.threadBoundCredentials.remove();
118118
}
119119

120120

org.springframework.jms/src/main/java/org/springframework/jms/connection/UserCredentialsConnectionFactoryAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,7 +131,7 @@ public void setCredentialsForCurrentThread(String username, String password) {
131131
* @see #setCredentialsForCurrentThread
132132
*/
133133
public void removeCredentialsFromCurrentThread() {
134-
this.threadBoundCredentials.set(null);
134+
this.threadBoundCredentials.remove();
135135
}
136136

137137

org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalSessionFactoryBean.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -776,19 +776,19 @@ else if (strategyAndRegion.length > 0) {
776776

777777
finally {
778778
if (dataSource != null) {
779-
configTimeDataSourceHolder.set(null);
779+
configTimeDataSourceHolder.remove();
780780
}
781781
if (this.jtaTransactionManager != null) {
782-
configTimeTransactionManagerHolder.set(null);
782+
configTimeTransactionManagerHolder.remove();
783783
}
784784
if (this.cacheRegionFactory != null) {
785-
configTimeCacheProviderHolder.set(null);
785+
configTimeCacheProviderHolder.remove();
786786
}
787787
if (this.cacheProvider != null) {
788-
configTimeCacheProviderHolder.set(null);
788+
configTimeCacheProviderHolder.remove();
789789
}
790790
if (this.lobHandler != null) {
791-
configTimeLobHandlerHolder.set(null);
791+
configTimeLobHandlerHolder.remove();
792792
}
793793
if (overrideClassLoader) {
794794
// Reset original thread context ClassLoader.
@@ -896,7 +896,7 @@ public void destroy() throws HibernateException {
896896
finally {
897897
if (dataSource != null) {
898898
// Reset DataSource holder.
899-
configTimeDataSourceHolder.set(null);
899+
configTimeDataSourceHolder.remove();
900900
}
901901
}
902902
}
@@ -942,7 +942,7 @@ public Object doInHibernate(Session session) throws HibernateException, SQLExcep
942942
}
943943
finally {
944944
if (dataSource != null) {
945-
configTimeDataSourceHolder.set(null);
945+
configTimeDataSourceHolder.remove();
946946
}
947947
}
948948
}
@@ -984,7 +984,7 @@ public Object doInHibernate(Session session) throws HibernateException, SQLExcep
984984
}
985985
finally {
986986
if (dataSource != null) {
987-
configTimeDataSourceHolder.set(null);
987+
configTimeDataSourceHolder.remove();
988988
}
989989
}
990990
}
@@ -1054,7 +1054,7 @@ public Object doInHibernate(Session session) throws HibernateException, SQLExcep
10541054
}
10551055
finally {
10561056
if (dataSource != null) {
1057-
configTimeDataSourceHolder.set(null);
1057+
configTimeDataSourceHolder.remove();
10581058
}
10591059
}
10601060
}

org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/SessionFactoryUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ public static void processDeferredClose(SessionFactory sessionFactory) {
735735
closeSession(session);
736736
}
737737
if (holderMap.isEmpty()) {
738-
deferredCloseHolder.set(null);
738+
deferredCloseHolder.remove();
739739
}
740740
}
741741

0 commit comments

Comments
 (0)