Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Improper use of singleton?

 
Balky BB
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jes Sie
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
frank,
why dont you write your feeling on why one should not use singleton for managing database connections
TIA
Manas
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"...
 
Manas Ahlaad
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja,
You like Frank much ! :roll:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic