On Wed, 22 Aug 2001, Gabriel J Zimmerman wrote:

>
> A follow up to my previous question:
>
> If a Singleton class is located in the WEB-INF/lib of one webapp and an
> identical class is also located in the WEB-INF/lib of another webapp,
> when the class is instantiated by one of the webapps, is the same class
> instantiation then automatically used for all the webapps?
>

No.  Classes are "global" only within the context of the classloader they
are loaded from (and any child class loaders, but that is not relevant
here), so each webapp gets their own copy of the Singleton.  Same goes for
static variables created within a class loaded from the webapp class
loader.

> Similarly, if a Singleton class is located in the $TOMCAT_HOME/lib
> directory and is instantiated by one of the webapps, is the same class
> instantiation then automatically used for all the webapps?
>

Yes.  In this case, the Singleton is shared across all web apps.

Technically, you are skating on slightly thin ice in relying on this,
because the spec does *not* require containers to provide a way to load
classes that are shared like this.  However, in practice, all containers
seem to have such a mechanism.

> Thanks again,
>
> Gabe Zimmerman
>

Craig


Reply via email to