Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton class for Closing Database Connections .

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


Is it good to have Database Utility class (A class for obtaining Database connections ) as a Singleton ??

Thanks in advance .

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What benefit do you think you would get from doing this?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a existing project , there the coding is like that , so the reason i asked the question .
so i want you to share your ideas on this .
 
Joe Ess
Bartender
Posts: 9319
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why a Singleton? Is there some reason you have to bottleneck all your close statements through a single instance?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you .
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are already such classes, pre-written, pre-debugged, and someone else is paying most of the time and expense of upkeep. One popular example is the Apache dbcp (Database Connection Pool), which is the default pooler for Tomcat, although I've also used it in stand-alone (non-web) applications.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't really a performance issue, but a design issue.

From a design standpoint database access code should be kept in one or at least in as few classes as possible. I wouldn't code it as a singleton as that isn't as flexible. You could certainly have static data structures that make the class similar to a singleton. Even if you use open source data access classes I would try to hide them in my own data access classes. This allows you to swap out that implementation if needed.
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
steve souza wrote:This isn't really a performance issue, but a design issue.
... Even if you use open source data access classes I would try to hide them in my own data access classes. This allows you to swap out that implementation if needed.


That brings up a good point. When you use a popular off-the-shelf product like apache jdbc it's also likely been optimized over its lifespan as well.

You don't really need to design "hiding" (a/k/a abstraction) classes for JDBC connection pools, however. The JDBC specs also define that particular mechanism (DataSource).

In the cases I've mentioned where I used Apache JDBC, the app was also using Spring, and since the abstracting mechanisms are already defined in and place, I used Spring to simply wire the database connection pooler into JPA with no custom code required at all.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside Hibernate , Since the creation of SessionFactory is expensive , is it good to mention this as a Singleton class so that we can get the same instance ?? and also will it not be a bottleneck for the Application ??

Is it good to have this class as a Singleton or not ??

Thnaks .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic