This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Performance and the fly likes Singleton class for Closing Database Connections . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Singleton class for Closing Database Connections . " Watch "Singleton class for Closing Database Connections . " New topic
Author

Singleton class for Closing Database Connections .

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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: 2234

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: 8834
    
    7

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: 2234

Thank you .
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

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.

Customer surveys are for companies who didn't pay proper attention to begin with.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
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: 15950
    
  19

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: 2234

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 .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton class for Closing Database Connections .
 
Similar Threads
connection to DB
Improper use of singleton?
Singleton in EJB
a mock quiz about Design Pattern
Singleton