Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With 2 web servers, will a singleton class have 2 instances?

Tags:

java

c#

oop

With 2 web servers, will a singleton class have 2 instances?

like image 364
Blankman Avatar asked Jun 25 '10 13:06

Blankman


People also ask

Can singleton class have multiple instances?

In software engineering, the multiton pattern is a design pattern which generalizes the singleton pattern. Whereas the singleton allows only one instance of a class to be created, the multiton pattern allows for the controlled creation of multiple instances, which it manages through the use of a map.

How many instances can a singleton class have at a time?

In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time.

Can there be multiple instance of singleton using cloning?

Cloning: Cloning is a concept to create duplicate objects. Using clone we can create copy of object. Suppose, we create clone of a singleton object, then it will create a copy that is there are two instances of a singleton class, hence the class is no more singleton.

Can we create 2 object of singleton class?

It falls under the category of the creational design pattern in Java. A Singleton class in Java allows only one instance to be created and provides global access to all other classes through this single object or instance.


1 Answers

Both the web-servers will have separate instances of their application processes be it .net or java. So Yes, both the servers will have their individual instances of your singleton class.

Regardless of the fact that these two web servers are two different physical machines, even if they are on the same server, they will definitely run entirely on different processes. Each process will load its objects in memory separately from any other process.

specifically in case of asp.net - Even in the single web server, each site will cause separate instance of the Singleton class. Because each site in asp.net worker process is loaded in separate application domain, no two domains can interfere between each others' objects. So in case of asp.net even the single web server having single asp.net worker process can/will have multiple instances of the singleton class each separate from another.

like image 153
this. __curious_geek Avatar answered Oct 04 '22 11:10

this. __curious_geek