• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

singleton class problem

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Their achilles heel is the noogie! Give them noogies tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic