tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: Singleton Revisited
Date Fri, 18 Aug 2000 19:02:14 GMT
Jim Rudnicki wrote:

> > For sharing information *within* a web app, the singleton pattern
> implemented
> > as a static variable is feasible and portable (although I dislike it for
> > "personal bias" reasons -- I've seen too many apps get messed up because
> any
> > code in the entire application can mess with those global variables,
> making
> > bugs hard to track down).
>
> Good, I thought that there would be only one classloader per webapp.  So all
> is well unless some decides to load balance for you and install the webapp
> on multiple machines.  Then the singleton would break again.
> What globals?  My singletons don't have any globals.
>

"Globals" in the sense I am talking about include singletons implemented as
public statics.  They are "global" in the sense that any Java code anywhere in
your app can access them, without requiring a reference to an object being
passed.

There is a class loader per web application.  In Java, "static" means "global
within a class loader".

Statics will "break" in a load balanced environment, but so would context
attributes.  You really end up having a copy of the singleton in each JVM.  Of
course, for something like a connection pool, this is exactly what you want to
have happen.

>
> Does a delegation model classloader break this?  Or, as long as the
> singleton is only one place (web-ing/classes) then the same loader will be
> used for the same webapp every time?
>

That's where life gets really interesting, because it depends on how the
application class loader is implemented.  In 2.2 there is considerable freedom
for container implementers (because the behavior is not specified).  This is
getting tightened quite a bit in 2.3 so there will be more predictability.

For Tomcat, as long as your classes are in either WEB-INF/classes or
WEB-INF/lib/*.jar, **and** there are no such classes on your system class path
(or placed in $TOMCAT_HOME/lib), then your "statics" will be global only within
the web app that loaded them.

>
> > For sharing information *across* web apps, the
> > servlet spec advises using external mechanisms (files, databases,
> directory
> > servers, etc.) to maintain portability.
>
> Okay, right, each webapp has a separate classloader.  But, this is good.
> You should not be able to share anything between webapps for security.
>

That's the basic reasoning in the servlet spec - another way to say it is that
web apps should be self contained (to the maximum degree possible) so that they
can be deployed on any container.

The ability to express a dependency on a particular external library, in a
portable way, is one of the other things being clarified in the 2.3 servlet
spec.

>
> Jim

Craig



Mime
View raw message