You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chad Lung <ch...@gmail.com> on 2007/09/05 04:18:01 UTC

Question on static variables

I have a unique situation where I'm building a polling server for a
Flash/Flex application.  I want to turn information around very quickly
between clients and therefore don't want to use a database if I don't have
to.  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'm expecting about 100 to 500
(max) clients at any given time.  What I'm curious about is how Tomcat
handles 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 kinda makes using static variables in ASP.NET and IIS a
bit tricky and unreliable.  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.

Thanks for any help on this.

RE: Question on static variables

Posted by Peter Crowther <Pe...@melandra.com>.
> From: Chad Lung [mailto:chad.lung@gmail.com] 
> 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 kinda makes using static variables in 
> ASP.NET and IIS a bit tricky and unreliable.

I know where you're coming from.  We routinely have to warn our clients
about IIS worker process recycling - it's a typical Microsoft approach
to reliability.  "Have you rebooted your application?"

Tomcat will reload a webapp if you have configured it to auto-reload and
it detects changes, or if it's directed to undeploy and redeploy the
webapp.  Tomcat is a single process and individual webapps don't get
recycled, although clearly Tomcat will also reload everything if it
crashes for any reason!

Tomcat will *not* reload classes placed in the common or shared
classloader directories, even if the webapp reloads.  If you're
deploying on servers you manage, that might be the best place to put the
class holding the static(s), as they'll then survive webapp reloads.
I'd be more cautious about doing this if you're creating a webapp for
customers to deploy, as it increases the deployment complexity and the
maintenance complexity.

		- Peter

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Question on static variables

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chad,

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG3id89CaO5/Lv0PARAvpjAJ98jJzq36367qFb/RaaGjDspFLpcACdH/NP
4FhYSHEYUx5HwggOBM2q/w4=
=wLEk
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org