• 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

JDBC connections in servlets

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to write a web-app using servlets (using "Java Servlets Developer's guide" by Karl Moss as a reference).
I have created my connection pool, established a connection, logged myself and moved on into my app.
My question deals with the creating and closing of the connections... In the example in the book the application is a one servlet class app (a login screen) and the connection, statment and resultset are created on doGet(), information is shown and then the these objects are all closed.
If I move past the login screen in my application, should i close the connection and then re-open it?!? seems like overkill - or can i assign my jdbc connection to the session?
What is the correct way to handle a connection between multiple screens. Think Im going to do some more reading, but any advice is appreciated.
Thanks,
Chris
 
Chris Brat
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
think i just found my own answer ... using a connection pool.
is it still correct to close the connection each time I leave a page tho ?!?
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
The subject of connecting to databases is one of great importance due to connections be so time consuming. Most application servers and containers come with in built facility for connection pooling. For example, I use Tomcat which has a JNDI Datasource.
What you might want to do is to have a Database class that takes care of the connection to the database and then use this whereever required.
HTH
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It is always a good practice to do the DB connectivity in the init() of servlet and by using the servlet instance pooling which many servlet containers and app servers support you need not worry abt the DB connectivity pooilng separately.
Cheers,
Gaya3
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is it still correct to close the connection each time I leave a page tho ?!?



Yes, you have to close the connection after finishing using it so that it is available to others.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic