aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Improper use of singleton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Improper use of singleton?" Watch "Improper use of singleton?" New topic
Author

Improper use of singleton?

Balky BB
Greenhorn

Joined: Nov 22, 2002
Posts: 3
We have a singleton class to encapsulate our datasource definitions. I believe this is valid since you would only want one class to initialize, then keep track of datasources.
Now I want to add a method (non-static) to utilize the datasources and execute calls against the database. This method would bind values to PreparedStatements, execute, then copy the resultSet to another data structure, then close the connections. In short -- it would handle client specific functionality. I think this is instance-specific and not class-specific behavior.
Can this be done in that Singleton class by a non-static method? Wouldn't simultaneous access interfere w/ each other ?
thanks.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
There are a lot of threads about the use of Singletons running in this forum at the moment. I'd recommend reading through them to get a picture of some of the issues surrounding the use of Singletons.
In the meanwhile, I have a question for you. Can you help us by explaining why you feel that an object of an ordinary, non-Singleton class is inappropriate for your use. Whey do you feel you need a Singleton?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
I would tend to agree that a singleton class should not usually 'bind values to PreparedStatements' or 'execute calls against the databse'.
Think of it this way. Since there is only one singleton object, you will probably end up with something that can only do one call against the database at a time. Or you will have to be careful to make all methods thread-safe.
But perhaps the class to 'manage the connection' is ok. Maybe you could have a non-singleton class to 'execute calls against the database', which takes or even contains the singleton class describing the database connection.
Jes Sie
Ranch Hand

Joined: Jul 24, 2001
Posts: 188
Originally posted by Michael Zalewski:

Think of it this way. Since there is only one singleton object, you will probably end up with something that can only do one call against the database at a time. Or you will have to be careful to make all methods thread-safe.

I always have thoughts that Singleton(say in Java env) does impose performance issues if so many other objects are relying on the singleton. Is this really true?
Appreciate if somebody would identify a thread on this issue.


Thank you.<br /> <br />- Jess Sie<br /> <a href="mailto:jess@see-consulting.com" rel="nofollow">jess@see-consulting.com</a>
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
why dont you implement a factory and make that return safe classes for you to interact with DB??
If I am completely off the track,please elaborate and explain your question.
Regds
Lupo


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
Jes Sie
Ranch Hand

Joined: Jul 24, 2001
Posts: 188
Originally posted by gautham kasinath:
why dont you implement a factory and make that return safe classes for you to interact with DB??
If I am completely off the track,please elaborate and explain your question.
Regds
Lupo

A factory and with a syncchronization constructor of the singleton is probably good. But, I'm still unsure of the performance....as per my previous posting. Any ideas there?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Syncronization certainly has a performance overhead. If you implement a singleton using the fairly common "lazy initialization", in a multi-threaded environement, you will incur the synchronization penalty of checking the locks everytime you fetch an instance.
You can usually trade space for time by pre-constructing a static instance, if you need it:

But I still have to ask. Why a Singleton at all ?
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
I think the problem is that you are trying to do too much in the singleton class.
Using a singleton class to register your database driver (or find your DataSource from JNDI) is OK. After all, you only want to do these things one time.
The problem is when you try to open a connection or execute a query. A singleton class for a database might return a java.sql.Connection with a method like public Connection getConnection(). But you probably will get into trouble if you try to use the singleton to wrap a Connection object.
Instead of trying to put a method into your singleton that would 'bind values to PreparedStatements', do one of the following:
1) Add a factory method to your singleton that returns a PreparedStatement. For example
<pre>MySingleton.getInstance().getPreparedStatement( "SELECT * FROM MyTable WHERE my_column=?")</pre>
Or
2) Wrap the PreparedStatement, and perhaps also CallableStatement into something like a Facade pattern. Then build a method in your singleton to return a new instance of this facade.
If you do either of these things, you won't have to worry about multiple threads, because most of your activity will occur in objects returned by your singleton. Each thread will have its own returned object. The getInstance method of your singleton class still has to be synchronized if you do lazy creation though.
Manas Ahlaad
Ranch Hand

Joined: Nov 07, 2001
Posts: 165
frank,
why dont you write your feeling on why one should not use singleton for managing database connections
TIA
Manas
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by manas ahlaad:
frank,
why dont you write your feeling on why one should not use singleton for managing database connections

Well, you should get a good overview by performing a google search like "Frank Carver Singleton"...


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
Manas Ahlaad
Ranch Hand

Joined: Nov 07, 2001
Posts: 165
Ilja,
You like Frank much ! :roll:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by manas ahlaad:
Ilja,
You like Frank much ! :roll:

Well, some of his messages read as if I *was* Frank. But I couldn't be possibly *that* mad, could I???
[ December 11, 2002: Message edited by: Ilja Preuss ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
But I couldn't be possibly *that* mad, could I???
I hope not. I'd be really disappointed to find I was just a figment of someone else's imagination.
Manas Ahlaad
Ranch Hand

Joined: Nov 07, 2001
Posts: 165
Any viewer of javaranch should agree, forget, I agree ,you both have been giving valuable suggestions and your participation and enthusiasm to help others is appreciable .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Improper use of singleton?