This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes ServiceLocator/Singleton as an cache in clustered environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ServiceLocator/Singleton as an cache in clustered environment" Watch "ServiceLocator/Singleton as an cache in clustered environment" New topic

ServiceLocator/Singleton as an cache in clustered environment

mik laan

Joined: Sep 04, 2002
Posts: 29
Does anyone know any articles or books considering Java/J2EE programming in clustered environment? I have many question in my mind but following is the most relevant.
Is it possible to use ServiceLocator/Singleton as an cache in clustered environment? I should maintain a collection of data in a Singleton class read from database using an EJB. That data could be changed by a Web client in applications' program code. What I have understood is that Singleton classes instances are different in every Clustered node's Web container. So, if this is true, how can I synchronize data if it's changing in one of the clustered node/Web Container?
Or is it possible to use EJB as an cache?
Anyone had this kind of situation...
Br Mik Laan
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Singleton's aren't singletons in a clustered environment. Whether that matters or not depends on what you use them for.
One use of a Singleton is to centralize access to some resources. The "singleness" is not critical, but is used to help with performance or memory or resource usage. It is OK to use this kind of singleton in a cluster, since having different instances on different machines is probably actually what you want, and doesn't affect the behavior.
Another kind of singleton would be one that caches read-only data. Here you want to reduce the overhead of creating/querying/reading the data. You only want to do this expensive operation once, so you cache the data in a singleton. This kind of singleton is also OK in a cluster, since it still meets the expectations (reducing expensive data access).
Another kind of singleton has some cache of read-write data. In this case, everybody that uses the singleton expects to get the correct current state. This kind of singleton, when implemented the traditional way using a static class variable, is bad in a cluster since each node of the cluster will have a different singleton instance, and they will get out of sync as soon as someone writes to it.
But you can still implement a cluster-wide singleton as long as it is backed by something better than a static class variable, like a database.
Probably the best fit for this would be an Entity Bean. The data is probably cached on each cluster node (depends on the app server), backed by a central database, and the app server may even keep the cluster node caches up to date with each other.
mik laan

Joined: Sep 04, 2002
Posts: 29
Thanks Dave!
Can anyone suggest any books etc. that tell more
about these kind of things in clustered programs.
This is quite interested subject,
Mik Laan
I agree. Here's the link:
subject: ServiceLocator/Singleton as an cache in clustered environment
Similar Threads
Sharing member variables in EntityBean instances?
Caching in an EJB container
database/session Problem
About ServiceLocator
Does service locator needs to be a Singleton to cache