File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Singleton for datasource? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Singleton for datasource?" Watch "Singleton for datasource?" New topic
Author

Singleton for datasource?

joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
I have a web application which uses Tomcat's connection pooling. I am paranoid about opening and closing connections efficiently. Does this approach seem reasonable? I use a singleton class to create a single datasource, then use that for opening connections:





Thanks for any feedback / reassurance!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What advantage do you think using a singleton gives you? Your code is stil open to the problem of developers forgetting to call DbConnection.close() is a safe way, so I'm not sure what it is you are trying to achieve by wrapping the DataSource and COnnection in your own classes.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
I was thinking in terms of efficiency - that it would be better to just have a single datasource object. I'm not totally clear on how Tomcat's pooling works, so this also seemed likely to ensure that there would just be one pool of connections, as they all come from the same datasource.

Am I on the wrong track?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The DataSource in Tomcat is JNDI bound is it not? In which case you probably are only using one instance anyway. So there is only going to be one connection pool. I think you might be worrying about something that is already taken care of.
[ January 23, 2007: Message edited by: Paul Sturrock ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Singleton for datasource?
 
Similar Threads
IBM Websphere 5 Connection Pool
Using datasource for login bean & login page?
datasource for mysql
closing pooled connection with WS naming service(jndi)
PLEASE HELP !!! WEBSPHERE CONNECTION POOLING PART II