Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton pattern?

 
Ed Dudley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My first post here - sorry if its a little complicated.

I'm designing an application which I want to store a chunk of XML in memory. All threads (servlets) can access this and it only changes infrequently.

I thought about using a singleton pattern for this (static stuff) but I've read that there can be problems with that when multiple VMs are used (eg. with clustering) as multiple versions of the object exist. If this is the case then when one object is updated it becomes out of sync with the objects in other VMs.

Is there a better way to do this?

My key requirement is that all VMs and threads can access an object and always get the same info (not necessarily the same object). Perhaps there is a way to store the info in a database and whenever an update is done signal all virtual machines to reconstruct the in memory object?

I hope this is clear! Sorry if its specific to my implementation but I guess the answers could be helpful to others too!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't an easy question! The usual answer is to store the data in a central location like a database or a specific server. Each VM grabs a copy of it and caches it for local speed. When one vm makes a change it pushes the change to the central location which keeps some form of version control ( usually a timestamp). Whenever a local vm wants to retrieve data from the local cache it first checks the version/timestamp with the central repository and if they are in synch just return the local data. Otherwise retrieve the newest version from the repository and update the local cache before returning.

An alternative is that each vm registers itself as a listener to the central repository and when one vm makes a change the repository pushes those changes to all listeners. If you look around the web you can probably find frameworks or products that provide the functionality.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ps. Welcome to JavaRanch!
 
Ed Dudley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve!

Having read a couple of other forums I'm considering creating a tasks table in the database with a date / time field. Then periodically polling the database to see if an update is needed.

I'll keep reading this thread though in case anyone else can suggest an another alternative to this.

Thanks again! I'll definitely be posting on this forum again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!