tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "kc.baltz" <kc.ba...@inforonics.com>
Subject RE: Logging to File
Date Tue, 09 May 2000 13:12:27 GMT


> -----Original Message-----
> From: Steve Weiss [mailto:sweiss@aamc.org]
> Sent: Monday, May 08, 2000 6:07 PM
> To: tomcat-user@jakarta.apache.org
> Subject: Re: Logging to File
> 
> 
> "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.
> 

When you say "not shared", you mean that it is possible to instantiate the
class once on each JVM, possibly creating more than one across the whole
"system", right?  

K.C.

Mime
View raw message