Singleton class using threads

Hi all,

I have one singleton class like Connection ( this has the databse connection). if everytime I need a connection I called the

Connection.getConnection() method.

But the problem is , in My applicaiton I am using the Threading.

If i use the threading then its getting the null pointer exception in the Connection class.

I think the Singleton class is using in the Normal process and also Thread process simultaniously. So How can I synchronize this connection with threading.

I used the getConnection method like below

public static synchronized cConnection getConnection()

{

return connection;

}

please suggest me.

thanks

[711 byte] By [nim_ramesha] at [2007-11-26 17:50:54]
# 1
Where and how is connection initialized? Or perhaps it's not which would explain the NPE. You did actually initialize the variable "connection" right?
kablaira at 2007-7-9 5:03:24 > top of Java-index,Java Essentials,Java Programming...
# 2
If multiple threads in an application are accessing a database, I would think this is a good candidate for a connection pool. What are your requirements?
DrLaszloJamfa at 2007-7-9 5:03:24 > top of Java-index,Java Essentials,Java Programming...
# 3

I am sorry i forgot to post that code

I intiated in the Main class.

public static synchronized void createConnection(String schema, String dsn,

String uid, String pwd)

{

if (connection == null)

{

connection = new cConnection(schema, dsn, uid, pwd);

}

}

nim_ramesha at 2007-7-9 5:03:24 > top of Java-index,Java Essentials,Java Programming...
# 4
Actually, Connection pool is better choice for threading.But we already developed the project without threading then later because of the speeding we included the threads.. now got the thread safe problem with singleton connection class
nim_ramesha at 2007-7-9 5:03:24 > top of Java-index,Java Essentials,Java Programming...
# 5
Problems are often a sign that you should fix your design. In good design "we already developed the project" is an excuse, not a reason.
DrLaszloJamfa at 2007-7-9 5:03:24 > top of Java-index,Java Essentials,Java Programming...
# 6
sorry actually we don't have time to re-design
nim_ramesha at 2007-7-9 5:03:24 > top of Java-index,Java Essentials,Java Programming...
# 7
Cleint need the fast solutions... thats why we are trying to solve as soon as possibleThanks
nim_ramesha at 2007-7-9 5:03:24 > top of Java-index,Java Essentials,Java Programming...