• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Storing DB access data in the DD

 
Vopli Vidoplyasova
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a small model 1 web application - several JSP's and Servlets and a database. I set up the DB connection in a context listener and save Connection object as a context attribute for the servlets to access (is that right?). Obviously I don't want to hardcode the db access stuff like url and password into the listener, so I was wondering is it safe to keep it in the deployment descriptor as context parameters or I should put it somewhere else?
Thank you!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slava Golovachenko wrote:I set up the DB connection in a context listener and save Connection object as a context attribute for the servlets to access (is that right?).

Not a good approach. This holds a connection open -- an expensive resource -- for the entire lifetime of the web application, and you are going to run into contention and synchronization issues.

You'd be much better off using a connection pool. Are you using Tomcat? It comes with any easy-to-use container-managed connection pool built right in.
 
Vopli Vidoplyasova
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Not a good approach. This holds a connection open -- an expensive resource -- for the entire lifetime of the web application, and you are going to run into contention and synchronization issues.

You'd be much better off using a connection pool. Are you using Tomcat? It comes with any easy-to-use container-managed connection pool built right in.

Thanks for the fast answer! No, I'm on GlassFish 3 right now. Gotta search for some info on using connection pools with it...
Still what about storing passwords in the DD, is it considered a good practice?
 
Vopli Vidoplyasova
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I successfully created a connection pool. I guess the question about passwords in the DD is not relevant anymore)) The only thing is, how do I access the connection from a servlet now? Also, do I need this listener at all if all I was using it for was setting up the connection?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic