• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing DB access data in the DD

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic