Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton for datasource?

 
joseph corner
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
joseph corner
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic