开发者

How are static objects treated while clustering?

Static objects are initialized only once. Single开发者_Python百科ton classes are instantiated only once. If we use a singleton in a cluster then it will create multiple instances of singleton in the clusters. So what happens to the static object in a cluster environment? Why this object is not initialized in other cluster servers? or why the objects state doesn't change?


Static Objects are always in the scope of the ClassLoader (in most cases per JVM) and are not regarded in clustering. If you need a Singleton you have to tell the container to create one. It depends on the nature of your Singleton if it should exist only one per Cluster or once per JVM or once per Classloader.


Each node in a cluster runs in a separate JVM - so each JVM (cluster node) will have it's own Singleton. If you look at the cluster as a system of JVMs, then it is true that the number of instances of a Singleton in the cluster is equal to the number of nodes.

A cluster wide Singleton can not be implemented with normal Java classes. You may need a single (un-clustered) server instances that provides the singleton.


Singletons aren't even reliable within a single process. You can load the same class via multiple classloaders and end up with multiple 'singleton' objects.

Singleton is an anti-pattern for a reason - avoid it.

The case is even worse in a cluster since all nodes must coordinate to decide where the singleton will be located. This is vulnerable to network partitioning so it's untenable. Brewer's CAP Theorem will give you some background on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜