This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Confused about how to handle conncetion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Confused about how to handle conncetion" Watch "Confused about how to handle conncetion" New topic

Confused about how to handle conncetion

Joel Hutters

Joined: Sep 07, 2006
Posts: 21
Hey all!
I have implemented my own Front Controller solution and it's now time to deal with database connectivity. I have read about some different solutions. First of all a basic questions:

Question 1: How does Driver.getConnection() work?
Does it return a new connection everytime it's invoked?

The Front Controller servlet invokes the the Driver.getConnection method in its init() method, and closes it in its destroy() method. The connection is then passed to my Action/Command objects.

Question 2: Does this mean that I only have one database connection for my web-app?

Is this a good solution? I have also read about having something called a BaseDao which is extended by other conrecte Daos' such as UserDao, NewsDao etc. This concrete classes invokes the super method getConnection, does it mean that I will get a new connection for every call?

I'm very confused about how to deal with the connections...
[ October 12, 2006: Message edited by: Joel Hutters ]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704

Have you considered database pooling? This is where you define a database connection on a server and then request a connection from within the application. The trick here is that your connection might come from any one of N database connections all ready available in the pool. The other trick when using database pooling is to make sure to close connections after you use them or you will use up the pool quickly.

My Blog: Down Home Country Coding with Scott Selikoff
Joel Hutters

Joined: Sep 07, 2006
Posts: 21
I actually implemented a connection pool solution with the built in support dbcp in apache. Works great together with mySQL
I agree. Here's the link:
subject: Confused about how to handle conncetion