File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton Pattern" Watch "Singleton Pattern" New topic
Author

Singleton Pattern

srilatha mahankali
Greenhorn

Joined: Nov 15, 2005
Posts: 6
Hi,

Could somebody Pl explain me how Singleton Pattern works when multiple requests are issues from different clients. I have this following ex which creates a connection to the database where i am using singleton pattern, i konw how to implement the pattern the thing i don't understand is how exactly it works. I am using the following code in a stand alone java application as well as in a web application so, now my q is in a stand alone env only one instance of ConnectionManager is created per JVM (per client) correct? In a web environment only one instance of ConnectionManager is created per request is that correct? According to the following code you can create no f connections through each instance of ConnectionManager class right then what is the point of Singleton i mean how do we limit the no f connections i am totally confused with this some body pl exaplin. I would greatly appreciate you guy's help in this.

Thanks,
Sree



package sd.integration;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/*
*
* This class uses singleton design pattern
*
* The datasource JNDI lookup is static for the application and is instantiated only once.
* This is used to get obtain/release connection to the database.
*
*/
public class ConnectionManager {

private static ConnectionManager instance = null;

DataSource sdDataSource = null;

public static synchronized ConnectionManager getInstance() {
if (instance == null) {
instance = new ConnectionManager();
}
return instance;
}

private ConnectionManager() {
try {
Context ctx = new InitialContext();
sdDataSource = (DataSource) ctx.lookup("jdbc/SDTDB");
} catch (Exception ex) {
//this is fatal so thow an Error
ex.printStackTrace();
throw new Error("DataSource JNDI lookup failed:".concat(ex.toString()));
}
}

public Connection getConnection() {
Connection connection = null;
try {
connection = sdDataSource.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
throw new Error("Get Connection error:".concat(ex.toString()));
}
return connection;
}

public void disposeConnection(Connection connection) {
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
throw new Error("Dispose Connection error:".concat(ex.toString()));
}
}
}
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I am using the following code in a stand alone java application as well as in a web application so, now my q is in a stand alone env only one instance of ConnectionManager is created per JVM (per client) correct? In a web environment only one instance of ConnectionManager is created per request is that correct?


This style of Singleton uses a static variable to store the actual ConnectionManager, so there is no more than one instance per classloader.

A typical standalone application just has one classloader, so in that case it's the same as one instance for the whole JVM.

A servlet container often has a classloader for each web application, so in that case there could be one ConnectionManager for each web application. Remember though, that a webapplication may have lots of requests and lots of sessions - all of these will share the web application's single ConnectionManager.

Has that helped at all?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
srilatha mahankali
Greenhorn

Joined: Nov 15, 2005
Posts: 6
Thanks Frank that makes sense. But i have one more q though, in case of a web application let's say that three different clients from three different computers are accessing my web application in that case three different ConnectionManger instances are created right?

One more thing each instance of ConnectionManager can create no f connections right how can we limit this. In my case on our Websphere app server max no f connections allowed per connection are 10 if it exceeds more thant that what happens.

Thank you so much for your time Farnk!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by srilatha mahankali:
But i have one more q though, in case of a web application let's say that three different clients from three different computers are accessing my web application in that case three different ConnectionManger instances are created right?


No. The Java code runs in the JVM of the web server, not on the client side. On the server, there is only one web application, serving all clients concurrently. So there will only be one ConnectionManager instance.

One more thing each instance of ConnectionManager can create no of connections right how can we limit this. In my case on our Websphere app server max no of connections allowed per connection are 10 if it exceeds more thant that what happens.


That depends solely on how the app server works. I'd guess that it throws an Exception, but it might even be configurable.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
srilatha mahankali
Greenhorn

Joined: Nov 15, 2005
Posts: 6
Thank You Ilja!

Just one more small q. Does that mean that an instance of ConnectionManager will be created first time the request comes in and when will this instance goes out of scope (Is it when the web application has been stopped).

Thanks Once Again!!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, the instance will be created the first time the getInstance() method gets called (as that is where it gets created, and only if the field is still null).

As the instance is referenced by a static field, it will only become eligible for GCing when the class gets GCed. The class won't get GCed before its ClassLoader gets GCed. And yes, you are correct, that likely won't happen before the Webapp gets stopped, unless you used your own Classloader to load the Singleton class.

Does that help?
srilatha mahankali
Greenhorn

Joined: Nov 15, 2005
Posts: 6
Thank You Ilja!!
Stanley Wong
Greenhorn

Joined: Feb 05, 2004
Posts: 3
What is the purpose/benefit to have singleton class?
If there are many requests to the class, does it process the request concurrently? Or it will process the request one by one?
Thanks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
That's up to how you write the code inside the Singleton.

All that the Singleton Pattern does is suggest ways to implement the idea of just one of something.
Mainak Goswami
Ranch Hand

Joined: May 21, 2006
Posts: 50

I was going through this post and I was wondering why do we need to create the ConnectionManager class as singleton?
If we use singleton then it means that the App server will create a single instance of the ConnectionManager class in the App server for that instance of the classloader.
What is the benefit? Is it that we are reducing multiple object creation and restricting the access to one user at a time? How is the ConnectionManager object shared between multiple users?

I can understand the usage of the Singleton Pattern for Print spooler or Logger where we are trying to restrict the usage to one at a time by creating one instance and also we can restrict the concurrent usage by using synchronized. Perhaps after Java 5 we can use enum etc.

It will be really helpful if somebody can explain this usage. Might be very basic which I am not able to understand.

Thanks,
Mainak

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Pattern