consistent use of JDK 1.5's ThreadLocal.remove() over ThreadLocal.set(null), preventing leaks (SPR-7441)

This commit is contained in:
Juergen Hoeller
2010-09-01 17:17:25 +00:00
parent 66a5bb7c22
commit e56cfb8173
14 changed files with 47 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -76,7 +76,12 @@ public abstract class AopContext {
*/
static Object setCurrentProxy(Object proxy) {
Object old = currentProxy.get();
currentProxy.set(proxy);
if (proxy != null) {
currentProxy.set(proxy);
}
else {
currentProxy.remove();
}
return old;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -106,7 +106,7 @@ public class ThreadLocalTargetSource extends AbstractPrototypeBasedTargetSource
this.targetSet.clear();
}
// Clear ThreadLocal, just in case.
this.targetInThread.set(null);
this.targetInThread.remove();
}

View File

@@ -904,13 +904,13 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
protected void afterPrototypeCreation(String beanName) {
Object curVal = this.prototypesCurrentlyInCreation.get();
if (curVal instanceof String) {
this.prototypesCurrentlyInCreation.set(null);
this.prototypesCurrentlyInCreation.remove();
}
else if (curVal instanceof Set) {
Set<String> beanNameSet = (Set<String>) curVal;
beanNameSet.remove(beanName);
if (beanNameSet.isEmpty()) {
this.prototypesCurrentlyInCreation.set(null);
this.prototypesCurrentlyInCreation.remove();
}
}
}

View File

@@ -344,7 +344,7 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.set(null);
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -496,16 +496,16 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
finally {
if (this.resourceLoader != null) {
configTimeResourceLoaderHolder.set(null);
configTimeResourceLoaderHolder.remove();
}
if (this.taskExecutor != null) {
configTimeTaskExecutorHolder.set(null);
configTimeTaskExecutorHolder.remove();
}
if (this.dataSource != null) {
configTimeDataSourceHolder.set(null);
configTimeDataSourceHolder.remove();
}
if (this.nonTransactionalDataSource != null) {
configTimeNonTransactionalDataSourceHolder.set(null);
configTimeNonTransactionalDataSourceHolder.remove();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,8 +51,8 @@ public abstract class LocaleContextHolder {
* Reset the LocaleContext for the current thread.
*/
public static void resetLocaleContext() {
localeContextHolder.set(null);
inheritableLocaleContextHolder.set(null);
localeContextHolder.remove();
inheritableLocaleContextHolder.remove();
}
/**
@@ -75,11 +75,11 @@ public abstract class LocaleContextHolder {
public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) {
if (inheritable) {
inheritableLocaleContextHolder.set(localeContext);
localeContextHolder.set(null);
localeContextHolder.remove();
}
else {
localeContextHolder.set(localeContext);
inheritableLocaleContextHolder.set(null);
inheritableLocaleContextHolder.remove();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -114,7 +114,7 @@ public class UserCredentialsDataSourceAdapter extends DelegatingDataSource {
* @see #setCredentialsForCurrentThread
*/
public void removeCredentialsFromCurrentThread() {
this.threadBoundCredentials.set(null);
this.threadBoundCredentials.remove();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -131,7 +131,7 @@ public class UserCredentialsConnectionFactoryAdapter
* @see #setCredentialsForCurrentThread
*/
public void removeCredentialsFromCurrentThread() {
this.threadBoundCredentials.set(null);
this.threadBoundCredentials.remove();
}

View File

@@ -776,19 +776,19 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
finally {
if (dataSource != null) {
configTimeDataSourceHolder.set(null);
configTimeDataSourceHolder.remove();
}
if (this.jtaTransactionManager != null) {
configTimeTransactionManagerHolder.set(null);
configTimeTransactionManagerHolder.remove();
}
if (this.cacheRegionFactory != null) {
configTimeCacheProviderHolder.set(null);
configTimeCacheProviderHolder.remove();
}
if (this.cacheProvider != null) {
configTimeCacheProviderHolder.set(null);
configTimeCacheProviderHolder.remove();
}
if (this.lobHandler != null) {
configTimeLobHandlerHolder.set(null);
configTimeLobHandlerHolder.remove();
}
if (overrideClassLoader) {
// Reset original thread context ClassLoader.
@@ -896,7 +896,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
finally {
if (dataSource != null) {
// Reset DataSource holder.
configTimeDataSourceHolder.set(null);
configTimeDataSourceHolder.remove();
}
}
}
@@ -942,7 +942,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
}
finally {
if (dataSource != null) {
configTimeDataSourceHolder.set(null);
configTimeDataSourceHolder.remove();
}
}
}
@@ -984,7 +984,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
}
finally {
if (dataSource != null) {
configTimeDataSourceHolder.set(null);
configTimeDataSourceHolder.remove();
}
}
}
@@ -1054,7 +1054,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
}
finally {
if (dataSource != null) {
configTimeDataSourceHolder.set(null);
configTimeDataSourceHolder.remove();
}
}
}

View File

@@ -735,7 +735,7 @@ public abstract class SessionFactoryUtils {
closeSession(session);
}
if (holderMap.isEmpty()) {
deferredCloseHolder.set(null);
deferredCloseHolder.remove();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -306,7 +306,7 @@ public class SqlMapClientFactoryBean implements FactoryBean<SqlMapClient>, Initi
finally {
if (this.lobHandler != null) {
// Reset LobHandler holder.
configTimeLobHandlerHolder.set(null);
configTimeLobHandlerHolder.remove();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -96,7 +96,7 @@ public class ConnectionSpecConnectionFactoryAdapter extends DelegatingConnection
* @see #setConnectionSpecForCurrentThread
*/
public void removeConnectionSpecFromCurrentThread() {
this.threadBoundSpec.set(null);
this.threadBoundSpec.remove();
}

View File

@@ -223,7 +223,7 @@ public abstract class TransactionSynchronizationManager {
Object value = map.remove(actualKey);
// Remove entire ThreadLocal if empty...
if (map.isEmpty()) {
resources.set(null);
resources.remove();
}
if (value != null && logger.isTraceEnabled()) {
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from thread [" +
@@ -314,7 +314,7 @@ public abstract class TransactionSynchronizationManager {
throw new IllegalStateException("Cannot deactivate transaction synchronization - not active");
}
logger.trace("Clearing transaction synchronization");
synchronizations.set(null);
synchronizations.remove();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,8 +57,8 @@ public abstract class RequestContextHolder {
* Reset the RequestAttributes for the current thread.
*/
public static void resetRequestAttributes() {
requestAttributesHolder.set(null);
inheritableRequestAttributesHolder.set(null);
requestAttributesHolder.remove();
inheritableRequestAttributesHolder.remove();
}
/**
@@ -80,11 +80,11 @@ public abstract class RequestContextHolder {
public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
if (inheritable) {
inheritableRequestAttributesHolder.set(attributes);
requestAttributesHolder.set(null);
requestAttributesHolder.remove();
}
else {
requestAttributesHolder.set(attributes);
inheritableRequestAttributesHolder.set(null);
inheritableRequestAttributesHolder.remove();
}
}