Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

Singleton in a cluster environment

hi guys

If there are two cache servers connected with a cluster and if i am using a singleton design pattern to create a singleton object, is it possible
that instance 1 is created in one server and instance 2 is created in another server ?

thanks.
Avatar of dpearson
dpearson

If you create a singleton, you're going to get one instance per JVM (or possibly per classloader if you're doing something fancy).

So on two different servers (which implies 2 different JVMs) you will get two different instances.

If you're saying these instances are being stored in a replicating cache then it would depend on the design of the caching software as to how it would handle this.

Doug
Avatar of Jay Roy

ASKER

thanks

One the contrary if i have one weblogic applicaiton server and i have 2 WAR projects deployed in it. Both the WAR projects have the same singleton java class. Will the singleton instance be in each of the WAR project or just one instance be used across both the WARS?

thanks
I think if you have 2 WAR files you will end up with 2 instances of the singleton.  That's because the goal is for each WAR to be completely isolated from the other - they shouldn't be able to communicate or affect each other.  If they shared the same singletons that wouldn't be the case.

Doug
Avatar of Jay Roy

ASKER

Alright,  but don't the two WARs share the same jvm if they are both deployed in the same application server?

Thx
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

Aahh, its clear now. So there are two aspects to it,
Class loaders and
The JVM itself

thanks.
Avatar of Jay Roy

ASKER

So I guess each (WAR)web-project will have its own class loader which will
Load the classes of that web-project when application server starts?
Yes that's right.  It allows the container to keep each project separate - which is usually the correct behavior.

Doug