You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Stefan Langer <ma...@web.de> on 2002/06/19 13:14:07 UTC

Are static vars VM dependend or ClassLoader dependend [offtopic]

Hello

This is a little off topic. According to documentation each webapplication
is loaded by a different classloader. If I understand this correct that
means each ServletContext is loaded with a different ClassLoader.
In my webapplication I'm using a singleton pattern to load some needed
ressources.
The static variable implementing the singleton is used by more than one
webapplication.
I'd like to know is this singleton a singleton in the ServletContext or is
it a singleton to the JVM? Stated differently is the static variable the
same for each webapplication in the Tomcat instance or does each Context
have its own copy of the static since each context is loaded by a different
classloader. As far as my testing has gotten I think that the static is only
available once in the Tomcat installation.
Can anybody confirm this or for that deny it??

Thank you very much in advance

Stefan Langer


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Are static vars VM dependend or ClassLoader dependend [offtopic]

Posted by Richard Plukker <ri...@bowtie.nl>.
Hello Stefan,

Based on my testing I can say this:
Webapps are loaded in a seperate class loader, each webapp has it's own.
(this goes for classes in the WEB-INF/classes and WEB-INF/lib)
If you use a singleton pattern it will be singleton for it's class 
loader (WebappClassLoader).
(more info classloader in tomcat docs).
therefor a singleton is not usefull if you want it to act a global 
singleton for each webapps.

However, I have been testing with crossContext, then you can use that 
way you can use classes from one context with another.
If you bind a Singleton to the JNDI context (look at the JNDI howto in 
tomcat docs) you should be able to make a Singleton global for webapps 
that are cross context.

How do you share an instance of an object now?

Good luck,
Richard

Stefan Langer wrote:

>Hello
>
>This is a little off topic. According to documentation each webapplication
>is loaded by a different classloader. If I understand this correct that
>means each ServletContext is loaded with a different ClassLoader.
>In my webapplication I'm using a singleton pattern to load some needed
>ressources.
>The static variable implementing the singleton is used by more than one
>webapplication.
>I'd like to know is this singleton a singleton in the ServletContext or is
>it a singleton to the JVM? Stated differently is the static variable the
>same for each webapplication in the Tomcat instance or does each Context
>have its own copy of the static since each context is loaded by a different
>classloader. As far as my testing has gotten I think that the static is only
>available once in the Tomcat installation.
>Can anybody confirm this or for that deny it??
>
>Thank you very much in advance
>
>Stefan Langer
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>  
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>