Two Java program asseccing the Singleton
I have implemented the Singleton design pattern. I want to know the life of this object (created by singlenton pattern). In the class hav a HashMap. A method** in other class that creat object and keep add enw entry in hashMap. Now i deploy this jar on Web sphere App Server. Now if i call method** 5 times from my web implementation in the object(singleton implementation) in the 5 time i got the all the values that are setted by previos 4 method call.
Now I have a class with main method that call the method**, now if i run this java program(with main method) from 5 diffrent command prompt simultaneously i dont get the values in HashMap that are stored by diff prog. Please help me why this is not happeneing and why hapening in web application. I think this is related to JVM, class loader and stuff, please let me know.
Please help me, in knowing the.
Manish
I am having some problems parsing.
You are running an application as a Web site, this then creates a single instance of a singleton correct?
But you then run it as 5 different command line application, and get five instances of your singleton?
That is normal behaviour. Your singleton is related to a instance of your application.
mlka at 2007-7-10 2:24:34 >

An object only exists within its JVM(1).
Run an application twice, get two JVMs.
Webservices are not "restarted" between each hit. Only one JVM is created.
1) Classloader, not JVM. But most apps only have a single class loader. Well, thats my excuse for being wrong, and I'm sticking to it.
Message was edited by:
mlk
mlka at 2007-7-10 2:24:34 >

> Both answers are yes.
>
> But why it is happening so i need technical reason.
Because a singleton's singularity is limited to the classloader that loaded its class. That's what "static" means in Java and "static" is at the heart of enforcing the singularity. One VM doesn't know anything about another VM's classloaders. Why would it? The two VMs are two entirely separate processes.