Avatar of royjayd
royjayd
 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.
Java

Avatar of undefined
Last Comment
royjayd

8/22/2022 - Mon
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
royjayd

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
dpearson

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
royjayd

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
dpearson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
royjayd

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

thanks.
royjayd

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
dpearson

Yes that's right.  It allows the container to keep each project separate - which is usually the correct behavior.

Doug
royjayd

ASKER
thanks
can you please help me with this question
https://www.experts-exchange.com/Programming/Languages/Java/Q_27734431.html