Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Instance

 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program to make a singleton instance.....in that singleton
class, i have a method to connect to the database, that would return a
connection object.

In my Client class, i am creating a singleton instance
for this class and am calling the method to connect to the database.


The code snippet for the getOnlyInstance() method



My concerns are...

1. Will the connection object be singleton?

2. Will it be accessible to multiple users?

3. Is this approach right?

4. When i use it in the servlet or some other program,
as Connection con = getConnection(), will it still remain singleton?

Though these are very trivial, i am a bit confused...that's why i want
to get it cleared....

Any help would he highly appreciated...

Cheers,
Swamy
 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program to make a singleton instance.....in that singleton
class, i have a method to connect to the database, that would return a
connection object.

In my Client class, i am creating a singleton instance
for this class and am calling the method to connect to the database.


The code snippet for the getOnlyInstance() method


code:
--------------------------------------------------------------------------------

public static SingleInstance getOnlyInstance() { //Check for existing instance and act accordingly }[CODE] The code snippet for the method getDatabaseConnection()[CODE] public static Connection getDatabaseConnection() { //Code to connect }[CODE] The code snippet for the creation of Singleton Instance : [CODE] SingleInstance myInstance = SingleInstance.getOnlyInstance();myInstance.getDatabaseConnection();

--------------------------------------------------------------------------------



My concerns are...

1. Will the connection object be singleton?

2. Will it be accessible to multiple users?

3. Is this approach right?

4. When i use it in the servlet or some other program,
as Connection con = getConnection(), will it still remain singleton?

Though these are very trivial, i am a bit confused...that's why i want
to get it cleared....

Any help would he highly appreciated...

Cheers,

Hi,

Answers to your questions:
1) The connection object will not be singlton.
2) No it will not be accessible to multiple users (assuming no connection pooling is involved at any level).
3) Generally speaking it is the right approach (Depends upon what you want to achieve)
4) No, even in 1st scenario it wasnt singleton, and as a result of your 4th question too it will not be singleton but a new connection per user request.

Note: Please close the connection from the client program before exiting the method.

Hope this helps.

Regards,
Mehul K. Sanghvi.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to implement the Singleton design pattern you have to have private constructor for that class and some public method which will return reference to itself.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Will the connection object be singleton?
No.
It is not-thread safe (fix it by synching the get method - note that double-check locking is broken).
Is exposes an implicit public constructor.

2. Will it be accessible to multiple users?
No.
It is not thread safe.

3. Is this approach right?
No.

4. When i use it in the servlet or some other program,
as Connection con = getConnection(), will it still remain singleton?
No. A servlet typically has concurrent access and so global data must be protected from race conditions.
Even if it were protected, it seems like an awful solution.
Consider using connection pooling; such as that provided by most containers as a DataSource.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple nits ...

Singleton pattern does not require a private constructor. Some singletons are single instance only because of programming conventions, eg servlets have public constructors but you know better than to use new on them. Singletons can also be obtained from by factories or registries or other interesting options instead of a getInstanceOfSelf kinda method.

The code snippets above don't tell us if Connection will be a singleton or not. A factory method getDatabaseConnection() could return a singleton connection, a new connection on every call, a connection from a pool, etc. etc.

What is the goal of this exercise? How many connections do you want to have in the world? What is the platform? You talk about client multiple users ... is the client a web server?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic