• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Singleton Pattern

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()));
}
}
}
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
srilatha mahankali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
srilatha mahankali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Ilja!!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Nothing? Or something? Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic