Are Tomcat webapp(s) class loaders ever re-created causing static variables to change?

B

brian

I have an interesting question (or at least to me it is). After
reading about the different type of class loaders (e.g. webapp(s),
System, Common, Shared, Ext.) in Tomcat and doing some testing I
noticed an interesting thing regarding a static variable within a
class that was loaded with the webapp class loader.

I have a class called JSPBean and within this bean I have a static
variable didLoadAppProperties.

public abstract class JSPBean {

private static boolean didLoadAppProperties = false;

// setter & getter removed to keep class short for posting.
}

I thought that if I set didLoadAppProperties = true from my JSP page
in my app the value would remain true for the life of the application
(unless I update it again or stop tomcat). However in my JSP page I
set the didLoadAppProperties = true and then wait a few days. If I go
to another JSP page in the same application a few days later I can
check the value of this static variable and it will be false. If I
print out the class loader on the page I notice that it has a
different class loader than the original one from three days ago.

Any ideas on if this theory is correct or why this might be happening
to me?
 
J

John C. Bollinger

brian said:
I have an interesting question (or at least to me it is). After
reading about the different type of class loaders (e.g. webapp(s),
System, Common, Shared, Ext.) in Tomcat and doing some testing I
noticed an interesting thing regarding a static variable within a
class that was loaded with the webapp class loader.

I have a class called JSPBean and within this bean I have a static
variable didLoadAppProperties.

public abstract class JSPBean {

private static boolean didLoadAppProperties = false;

// setter & getter removed to keep class short for posting.
}

I thought that if I set didLoadAppProperties = true from my JSP page
in my app the value would remain true for the life of the application
(unless I update it again or stop tomcat). However in my JSP page I
set the didLoadAppProperties = true and then wait a few days. If I go
to another JSP page in the same application a few days later I can
check the value of this static variable and it will be false. If I
print out the class loader on the page I notice that it has a
different class loader than the original one from three days ago.

Any ideas on if this theory is correct or why this might be happening
to me?

A class, and hence its static members, is scoped to the particular
ClassLoader that loaded it. If a different ClassLoader loads the class
then there are two distinct copies, each with its own distinct static
members. I have not studied Tomcat's docs well enough to propose a
specific theory on what happened to you, but I can imagine a number of
general scenarios in which the application might end up loading the same
class multiple times with different ClassLoaders. (In most of them the
application would only have one copy of the class at a time.)

It comes down to this recommendation: do not depend on static variables
in any environment that makes creative use of ClassLoaders. Application
servers are generally such environments. These environments often have
alternative, more reliable means for storing application-wide data; in
the case of JSP / Servlets the best means is probably to set an
attribute on the ServletContext (aka create an application-scope
scripting variable).


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top