Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DB Connections and Exception Handling in Web Application

 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion it's better to configure a connection pool in the application server, rather than making your own connections in any of the applications.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Sahil ,

Why wont you try Spring Framework which does all low level job like opening and closing the db connections and
infact it has api which are wrapped over the core JDBC concepts .
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic