tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Question on static variables
Date Wed, 05 Sep 2007 03:50:20 GMT
Hash: SHA1


Chad Lung wrote:
> What I want to do is store information in a few static variables used
> in a servlet.  I'm looking for speed more than anything as the clients will
> be polling the server every 2-3 seconds.

I think what you really want is to store that information in the
"application" context, also known as the ServletContext. Any servlet can
get access to the ServletContext for any request.

You can use a ServletContextListener to listen for application startup
events to, say, load the data once from a database or flat file or whatever.

> What I'm curious about is how Tomcat
> handles static variables.

Tomcat does not support any special handling of static variables.

> Coming from ASP.NET experience I know that using
> static variables with IIS they are held in the app domain and the app domain
> can be reloaded by IIS for a number of reasons (config files changed, files
> changed, etc.)

This is sort of like the ServletContext, but in behavior only. Tomcat
discards the web application (context) when you redeploy, but the JVM
can hold onto references to the now-useless memory if you're not careful
about how you manage your memory. If you leave references to objects
from the old webapp around, you can cause the old webapp to stick around
in memory.

> This kinda makes using static variables in ASP.NET and IIS a
> bit tricky and unreliable.

Exactly. Same with Java and webapps.

> So the question I have is how does Tomcat handle
> static variables then?  When would they get reloaded/recycled, etc.?  I'm
> aware I could use something like memcached to mimic what I'm doing but again
> I'd rather stick to static variables if possible for the sake of simplicity.

Using the ServletContext to store information is easy, convenient, and
pretty safe. That's my recommendation. You could also take a look at the
build-in database that comes with the newer JVMs. I think there's an
in-memory implementation if you have complex data needs but still want
the speed of RAM.

- -chris
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla -


To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message