Browse Source

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

3.0.x
Juergen Hoeller 16 years ago
parent
commit
e56cfb8173
  1. 7
      org.springframework.aop/src/main/java/org/springframework/aop/framework/AopContext.java
  2. 4
      org.springframework.aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSource.java
  3. 4
      org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
  4. 2
      org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java
  5. 10
      org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java
  6. 10
      org.springframework.context/src/main/java/org/springframework/context/i18n/LocaleContextHolder.java
  7. 4
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/UserCredentialsDataSourceAdapter.java
  8. 4
      org.springframework.jms/src/main/java/org/springframework/jms/connection/UserCredentialsConnectionFactoryAdapter.java
  9. 18
      org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalSessionFactoryBean.java
  10. 2
      org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/SessionFactoryUtils.java
  11. 4
      org.springframework.orm/src/main/java/org/springframework/orm/ibatis/SqlMapClientFactoryBean.java
  12. 4
      org.springframework.transaction/src/main/java/org/springframework/jca/cci/connection/ConnectionSpecConnectionFactoryAdapter.java
  13. 4
      org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java
  14. 10
      org.springframework.web/src/main/java/org/springframework/web/context/request/RequestContextHolder.java

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

@ -1,5 +1,5 @@ @@ -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 { @@ -76,7 +76,12 @@ public abstract class AopContext {
*/
static Object setCurrentProxy(Object proxy) {
Object old = currentProxy.get();
if (proxy != null) {
currentProxy.set(proxy);
}
else {
currentProxy.remove();
}
return old;
}

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

@ -1,5 +1,5 @@ @@ -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 @@ -106,7 +106,7 @@ public class ThreadLocalTargetSource extends AbstractPrototypeBasedTargetSource
this.targetSet.clear();
}
// Clear ThreadLocal, just in case.
this.targetInThread.set(null);
this.targetInThread.remove();
}

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

@ -904,13 +904,13 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp @@ -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();
}
}
}

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

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

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

@ -1,5 +1,5 @@ @@ -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 @@ -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();
}
}

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

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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();
}
}

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

@ -1,5 +1,5 @@ @@ -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 { @@ -114,7 +114,7 @@ public class UserCredentialsDataSourceAdapter extends DelegatingDataSource {
* @see #setCredentialsForCurrentThread
*/
public void removeCredentialsFromCurrentThread() {
this.threadBoundCredentials.set(null);
this.threadBoundCredentials.remove();
}

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

@ -1,5 +1,5 @@ @@ -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 @@ -131,7 +131,7 @@ public class UserCredentialsConnectionFactoryAdapter
* @see #setCredentialsForCurrentThread
*/
public void removeCredentialsFromCurrentThread() {
this.threadBoundCredentials.set(null);
this.threadBoundCredentials.remove();
}

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

@ -776,19 +776,19 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen @@ -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 @@ -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 @@ -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 @@ -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 @@ -1054,7 +1054,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
}
finally {
if (dataSource != null) {
configTimeDataSourceHolder.set(null);
configTimeDataSourceHolder.remove();
}
}
}

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

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

4
org.springframework.orm/src/main/java/org/springframework/orm/ibatis/SqlMapClientFactoryBean.java

@ -1,5 +1,5 @@ @@ -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 @@ -306,7 +306,7 @@ public class SqlMapClientFactoryBean implements FactoryBean<SqlMapClient>, Initi
finally {
if (this.lobHandler != null) {
// Reset LobHandler holder.
configTimeLobHandlerHolder.set(null);
configTimeLobHandlerHolder.remove();
}
}
}

4
org.springframework.transaction/src/main/java/org/springframework/jca/cci/connection/ConnectionSpecConnectionFactoryAdapter.java

@ -1,5 +1,5 @@ @@ -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 @@ -96,7 +96,7 @@ public class ConnectionSpecConnectionFactoryAdapter extends DelegatingConnection
* @see #setConnectionSpecForCurrentThread
*/
public void removeConnectionSpecFromCurrentThread() {
this.threadBoundSpec.set(null);
this.threadBoundSpec.remove();
}

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

@ -223,7 +223,7 @@ public abstract class TransactionSynchronizationManager { @@ -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 { @@ -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();
}

10
org.springframework.web/src/main/java/org/springframework/web/context/request/RequestContextHolder.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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();
}
}

Loading…
Cancel
Save