On stop
or undeploy/redeploy
of a Spring framework 3.0.5
based web application following error is logged in Tomcat7's catalina.out
:
SEVERE: The web application [/nomination##1.0-qa] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@4f43af8f]) and a value of type [org.springframework.security.core.context.SecurityContextImpl] (value [org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
I initially thought of implementing ServletContextListener
and close()
the context there. However, found ContextLoaderListener
which implements ServletContextListener
is set up like so in web.xml
:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
From Javadocs:
**contextDestroyed**
public void contextDestroyed(ServletContextEvent event)
Close the root web application context.
Specified by:
contextDestroyed in interface ServletContextListener
So, my question is why the ThreadLocal not cleanly freed by ContextLoaderListener.contextDestroyed()
?
We are running into PermGen
errors and while investigating found this. There is code similar to the following in a few places:
ApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(se.getSession().getServletContext());
MyBeanClass x = context.getBean(
"myBean", MyBeanClass.class);
x.someMethod();
I wonder if the above snippet is stopping a clean shutdown? Any pointers will be greatly appreciated.