File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
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: 3753

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.

[OCA 8 Book] [Blog]
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
wood burning stoves
subject: Confused about how to handle conncetion
It's not a secret anymore!