tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Weiss <swe...@aamc.org>
Subject Re: Logging to File
Date Mon, 08 May 2000 22:06:52 GMT
"kc.baltz" wrote:
> 
> > I would like to initialize some Application wide objects the
> > first time any
> > Servlet or JSP page is loaded.  I would like to create one
> > instance of the
> > logger, and a connection pool for interacting with an XML
> > datasource.  Is there
> > a way to do this without placing code in every single Servlet
> > and JSP page to
> > ensure that initialization has taken place?
> >
> 
> Yes, it's a software design pattern called the Singleton.  You create a
> class like the following:
> 
> <pre>
> class OneInstance {
> 
>         private OneInstance(/* Constructor parameters */) {
>                 // the only constructor is private
>         }
> 
>         public static OneInstance getInstance(/* Constructor parameters */)
> {
>                 // See if this class has been allocated yet.
>                 if( _instance == null ) {
>                         _instance = new OneInstance();
>                 }
>                 return _instance;
>         }
> 
>         private static OneInstance _instance = null;
> }
> </pre>
> 
> Any servlet/JSP that needs a copy of this just calls getInstance instead of
> a constructor.  This way you only get one instance per JVM.  Anyone know how
> this works with 2 JVMs like when you have an RMI server?
> 

In my case, this is in fact what I have (2 RMI servers, each running in
it's own JVM + Tomcat). The singleton objects aren't shared across JVM
boundaries.

-Steve
-- 
Steve Weiss      Association of American Medical Colleges
(202)828-0428    mailto:sweiss@aamc.org    http://www.aamc.org

Mime
View raw message