| Author |
Singleton class for Closing Database Connections .
|
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2229
|
|
Hi
Is it good to have Database Utility class (A class for obtaining Database connections ) as a Singleton ??
Thanks in advance .
|
Save India From Corruption - Anna Hazare.
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
What benefit do you think you would get from doing this?
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2229
|
|
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
Joined: Oct 29, 2001
Posts: 7796
|
|
|
Why a Singleton? Is there some reason you have to bottleneck all your close statements through a single instance?
|
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2229
|
|
|
Thank you .
|
 |
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 11740
|
|
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.
|
A lot the of modern-day software development platforms are designed to permit parcelling out work to those with the best aptitude for it. A lot of modern-day business is predicated on making one person do all the work, regardless of aptitude.
|
 |
steve souza
Ranch Hand
Joined: Jun 26, 2002
Posts: 845
|
|
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.
|
http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
|
 |
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 11740
|
|
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
Joined: Apr 18, 2009
Posts: 2229
|
|
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 .
|
 |
 |
|
|
subject: Singleton class for Closing Database Connections .
|
|
|