Randy Belknap wrote:

> I've written an application that uses a number of singleton classes to for
> things like database access, etc.  I've also put some application state in
> these singletons.  (For example, if someone updates a particular table, I
> set a time field indicating when the data was last updated and servlets can
> use the time field to determine whether to refresh data.  This is just one
> example)
>

As an alternative to singleton classes, you might also consider storing these
objects in the ServletContext via the setAttribute() method.  Such objects are
always available to your servlet via:

    getServletContext().getAttribute("KEY");

and, as an extra benefit, you can run more than one application on the same
server if you need to (each in its own ServletContext), without running into
name clashes on your Singleton classes.

>
> Am I hosed if I go to a clustered environment?  It seems like each app server
> will have its own instance of the singleton classes.
>

Depends on which server you are running.  Some app servers will make promises to
try to replicate objects stored in the servlet context across your cluster, if
the objects implement Serializable or can otherwise be transported.  You can
pretty much count on this not being supported for your Singleton objects.

>
> Is there a way to store application state short of writing it to a common
> data store that is accessed by the singletons?
>

Either you have to do it, or your app server has to do it for you somehow.

>
> TIA,
>
> Randy
>

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to