Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connection to database in a Bean or Servlet?

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I was reading some of the latest posts and I found this one:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=7&t=007630
here, it uses a bean to make a connection to a database. Then you have a servlet that receives some parameters and perform a particular task using the bean. My question is:
What is the best practice, to have all the process of connecting to a database in a servlet or JavaBean..?
thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one thing, use a connection pool instead of trying to manage db connections yourself. Your servlet must be designed so that connections are returned to the pool no matter what happens.
Bill
 
George Carlson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best implementation of this is to have one class that both your servlets and jsp's have access to for each table in your database with variables corresponding to each field in your database. Each time you need access to the database, use only this class. That way, you can synchronize on that class and you'll never have to worry about having bad data, especially when adding new data or updating. Just doing selects, it isn't as big of a deal, but it is still much more managable having just one class that both servlets and the jsp has access to.
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sortsol,
Your display name does not conform to the Javaranch naming policy. Please read this policy and change your display name if you wish to continue posting here. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic