File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to handle db connections in servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to handle db connections in servlets?" Watch "How to handle db connections in servlets?" New topic

How to handle db connections in servlets?

Amudha Loganathan

Joined: May 20, 2002
Posts: 6
How to efficiently handle db connections in servlets deployed in WAS4.0?
In servlet init method I am opening the connection and I am closing it in the destroy method ?
Is it correct ?

TAI,<br />Amudha L
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I don't know if Websphere does it's own connection pooling. You may want to look into it though. Since it means that several hits to you servlet will share the same connection.

So much trouble in the world -- Bob Marley
Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
For a DB connection pool initialization the best means would be as
follows in decreasing order of preferance
1. Context Initialization
2. init() method
3. initialization on the receiving the first request.
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Use a DataSource. IBM has a good example of setting this up at Advanced application assembly and deployment tutorial
Once it's configured for your server, any example code for using a Connection from a DataSource will work.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
I agree. Here's the link:
subject: How to handle db connections in servlets?
It's not a secret anymore!