tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig McClanahan <Craig.McClana...@eng.sun.com>
Subject Re: Singleton Revisited
Date Wed, 16 Aug 2000 16:15:02 GMT
Jim Rudnicki wrote:

>
> > 2) multiple instances of the "singleton" can get made as more than one
> class
> > loader and/or VM are involved;
>
> Not an easy way around this.  If you have one server, one jvm, one
> classloader. then I would hope that the singleton holds up.  Now you have me
> all worried.

Even in a single-JVM environment, a typical servlet container implementing the
2.2 spec will have more than one class loader defined.  For example, Tomcat
defines a class loader per web application -- that is how it implements making
classes under WEB-INF/classes and WEB-INF/lib available to each particular app.

The behavior of statics (in particular, whether they are global across web apps
or not) depends on which class loader loaded that class, and therefore depend
on how your particular servlet container does this.  You cannot count on
consistency across platforms.

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).  For sharing information *across* web apps, the
servlet spec advises using external mechanisms (files, databases, directory
servers, etc.) to maintain portability.

Craig



Mime
View raw message