Singleton pattern not working properly

Hi all,

I am having a small problem in my web application, My requirement is,

1. I need to fetch a set of values from db and spawn a thread to a jsp which will process the values simultaneously.

2. I am using a static list in a class with singleton pattern

3. I am loading the list with data

4. Spawning the threads which will call the jsp.

5. The jsp will fetch the data stored in the list

6. In this process, while I am reteriving the values from the list from jsp, The singleton class gets instantiated again and so I am getting an empty list.

I am not sure what goes wrong here. Please suggest if anybody has a clue what is wrong in above process. Also suggest if you have any other idea for acheiving the same instead of storing in List.

Thanks and Regards,

vignesh

[838 byte] By [MysunScreena] at [2007-11-27 5:00:37]
# 1

singleton code:

private static abcde abcdeInstance;

public static abcde getInstance()

{

if(abcdeInstance == null)

{

abcdeInstance = new abcde();

}

return abcdeInstance;

}

Then, when you invoke,

abcde myClass = abcde.getInstance();

skp71a at 2007-7-12 10:17:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

The other thing of note about implementing the singleton pattern is that you make the constructor private as well. That way, you can only instantiate the object from inside the class, and ensure that only one gets created (disregarding thread/synchronisation issues)

An alternative to the singleton pattern would be to use the server's application scope. That is shared memory for all users.

evnafetsa at 2007-7-12 10:17:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

The given code example using the null check is not threadsafe. Rather use a private constructor and instantiate directly in the private static property.

public class SingleTon {

private static SingleTon instance = new SingleTon();

private SingleTon() {

// Hide constructor.

}

public SingleTon getInstance() {

return instance;

}

}

BalusCa at 2007-7-12 10:17:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi all,

Thanks for the replies. I am using the singleton pattern as suggested by you all. I am giving my sample code :

public static String str=null;

private static TestSingleton tst=null;

public static TestSingleton getInstance(){

if(tst==null){

tst=new TestSingleton();

System.out.println("Calling new instance...");

}

return tst;

}

public void setStr(){

str="Hi";

System.out.println("STR:"+str);

}

public String getStr(){

return str;

}

and I am calling the setStr() method as shown below:

public static void main(String[] args) {

TestSingleton tst=TestSingleton.getInstance();

tst.setStr();

Threadt=new Thread(new Update1());

t.start();

}

In the Update thread, I am calling a jsp where I am calling a getter method:

<%@ page language="java"

import="<Imports>"

%>

<%

try{

System.out.println("Inside TestX.jsp..");

TestSingleton tst=TestSingleton.getInstance();

System.out.println(tst.getStr());

}catch(Exception e){

e.printStackTrace();

}

%>

But the problem I am facing is:

while calling the getInstance in main method , the TestSingleton object is initialized once. I am setting str to Hi in here. But in jsp when I am calling the getInstance method, to get the setted value, The TestSingleton object gets reinitialized and so I am getting null while calling getStr() method in jsp

Please help if anybody knows whats wrong with the approach above.

Thanks & Regards,

vignesh

MysunScreena at 2007-7-12 10:17:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Does it re-call the getInstance() every time you access the JSP?

The "Singleton" gives you a single instance per jvm/classloader.

At a guess, your "main" program is being invoked by a different process than your jsp. Therefore they don't share the same jvm/classloader, and thus both of them will instantiate the class. As far as each process is aware it IS the first time you are invoking the class.

If you are trying to load and store data into memory in the Application server, you will have to start up that thread process within the app server.

One way to do that is to define a servlet that is "loadOnStartup", and in its init, start the process you want running in the background.

Hope this helps,

evnafets

evnafetsa at 2007-7-12 10:17:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...