File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Persisting singletons to disk during shutdown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Persisting singletons to disk during shutdown" Watch "Persisting singletons to disk during shutdown" New topic
Author

Persisting singletons to disk during shutdown

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

I create a singleton class which maintains a running number (monotonically increasing). The class exposes a public method next.
If I make the class implement the Serializable
interface, will the servlet engine persist it
automatically to the hard disk on shutdown and
subsequently restore it at bootup ? And more
importantly, the singleton class should continue
farming out serial numbers from its previous
state before the shutdown.
Thanks
P/S I am using WebSphere
Pho


Regards,

Pho
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Not entirely sure, don't think so. But it seems to me that the answer really doesn't matter -- you can never rely on the servlet container calling destroy() and performing a proper shutdown. The machine or JVM might simply crash, the power might be cut...
Also, have you considered that the approach will not work at all in a distributed container?
Furthermore, when using the Singleton feature in a web-app you should realise that during development, due to class reloading, there may well be more than one version of your Class living in the JVM and therefore more than one version of your Singleton. To address this you can bind the singleton instance in the servlet context rather than in a static variable (but beware of ClassCastExceptions for reloaded non-system classes).
- Peter
[ May 09, 2002: Message edited by: Peter den Haan ]
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Peter,
Thanks for replying. Please also read my
other post on my singleton implementation
(if you have time). I would love to hear
your critique on my design.
I am aware of the problems that you mentioned
specifically about multiple instances of the singletons. Does anyone know of a way to generate
monotonically increasing numbers in a multithreaded environment and that can be persisted ? These numbers are being used as primary keys in my tables.
Another solution I've thought of is to do everything in the database. e.g. a table for counters or use proprietary data types e.g. IDENTITY constraint in SQLServer or SERIAL/SERIAL8 data type in Informix IDS.
Cheers,
Pho
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

I think you've just found a good use for an entity EJB.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

Actually, I shouldn't have said "think". I do in fact use this method often. I have a "key table" in the DBMS that pairs table names with next-up key values and use that to back an EJB that serves up the new key values for the indicated DBMS tables.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Have a search and you will find various EJB-based (and otherwise-based) number generators on the 'net.
If the numbers do not need to be contiguous (which they won't be for most primary-key-sequence-generator applications), the most powerful pattern is one whereby an EJB (or Java class) allocates a batch of 10... 100... or perhaps 1000 numbers from the nextnum table, then hands them out one by one. The advantage is that there may be multiple generators in multiple JVMs, and the database still won't be a bottleneck. The disadvantage is that some numbers will go unused when the application shuts down or crashes.
Again, do a search and you'll find implementations. I seem to remember discussions on TheServerSide ages ago.
- Peter
 
Don't get me started about those stupid light bulbs.
 
subject: Persisting singletons to disk during shutdown