• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton class problem

 
shikhar singh
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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..
Shikhar
 
David Weitzman
Ranch Hand
Posts: 1365
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic