Currently I'm creating the DB connection again and again in every JSP/Servlet. I would like to reuse my DB connection in my JSP/Servlet project. How can I achieve this?
-
4JEE has native support for connection pools. What's your application server?JB Nizet– JB Nizet2012-09-20 10:18:47 +00:00Commented Sep 20, 2012 at 10:18
-
1check out this stackoverflow.com/questions/2313197/…MaVRoSCy– MaVRoSCy2012-09-20 10:32:24 +00:00Commented Sep 20, 2012 at 10:32
-
@JBNizet Am using tomcat v7.0Raj Inevitable– Raj Inevitable2012-09-20 10:39:53 +00:00Commented Sep 20, 2012 at 10:39
-
2tomcat.apache.org/tomcat-7.0-doc/…JB Nizet– JB Nizet2012-09-20 10:42:51 +00:00Commented Sep 20, 2012 at 10:42
Add a comment
|
1 Answer
The "correct" way to do this is to make use of connection pooling. That way the overhead of creating / closing connections is reduced.
Apart from that there's is nothing wrong with the way you are doing things. Maintaining open connections for too long is generally not a good idea.
To make this less hassle - you can create a utility class which returns a connection from the pool and likewise has a method for returning the connection to the pool.