DB Connections and Exception Handling in Web Application
Joined: Aug 27, 2003
I would like to know in a simple web application built using Struts and DAO. How one should do the exception handling and database connections. Should all the DAO class create their own connections and close them or is it better to have a seperate class to handle this.
Using a connection pool that's managed by the container is really the only good way to deal with database connections in a web application. Opening a database connection is a relatively slow and heavy operation. Your webapp will become very slow if you open a new connection for each request when the app needs to use the database. It would also make your webapp unable to scale up to large numbers of users, because databases can normally handle only a limited number of connections at a time. A connection pool manages all this for you.
What you'd normally do is setup a connection pool in your container, and then in your webapp get a javax.sql.DataSource object via JNDI, from which you can get the database connection. Here's a tutorial for how to do that with Tomcat.