jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes singleton class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "singleton class problem" Watch "singleton class problem" New topic

singleton class problem

shikhar singh
Ranch Hand

Joined: Oct 02, 2000
Posts: 31
Working on a code where I will provide a class to read/update some attributes.
Options with me are:
Store in DB
Store in Property File
Store in an Interface.
I decided to go for the Property File.
I thought I will make a singleton class and will read the attributes during the loading of class. Because there will be a silgle instance every call (read or update) will go through it.
Everything going fine. But hey!! In distributed env where we can have more then more then one JVM, how this is going to work.
Is there any work around?
Then I thought, drop the idea of singleton class, I will read the attributes from the property file and everytime somebody trys to read a attribute, I'll check the timestamp of the file. If it is different from the previous one, Update the properties.
Is this the right approach? Isn't this going to be performance overhead. I know this might be a common problem.
Is there any alternate or better way to go about this.
Would appreciate your response..
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
You can't really avoid a race condition when you've got multiple VM's involved, so I'd say it's probably unsafe. You might wanna try some sort of file based Reader-Writer lock or RMI.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
File.createNewFile() is atomic, so you can use it to implement reliable inter-JVM file locking when accessing the properties file. It won't be mind-boggingly efficient but it may well be efficient enough.
But really, if this file is going to be updated by multiple processes a DB-based solution would've been the best way to go.
- Peter
I agree. Here's the link: http://aspose.com/file-tools
subject: singleton class problem
It's not a secret anymore!