This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Singleton Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton Instance" Watch "Singleton Instance" New topic
Author

Singleton Instance

Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
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

Joined: Feb 04, 2002
Posts: 134
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

Joined: Aug 04, 2004
Posts: 724
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.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Instance