Helpful Information
 
 
Category: Java Help
Database Connectivity in Servlets

Hi,

I have a problem in maintaining the open database connection objects in servlets. How to do this? Once, i connect to the database from one servlet or page. It should be opened for all the servlets which uses sql statements. Every servlet or page should not make a new connection.

Vijaya.

hi there,

you can create a connection in one servlet..
then put in into ServletContext.
e.g. In "AServlet.class" :

Connection con = .....
ServletContext sc = getServletConfig().getServletContext();
sc.setAttribute("con", con);

and in "BServlet.class", you can retrieve this connection
by doing:

Connection con = null;
ServletContext sc = getServletConfig().getServletContext();
con = (Connection)sc.getAttribute("con");

and do whatever you want in "BServlet.class".
try it... it'd better to create a ConnectionPool class
to give out connection... therefore each servlet
will get their own connection and return it back to
the ConnectionPool once they finish.

good luck.
-hoi.










privacy (GDPR)