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?
Asked
Active
Viewed 1,188 times
1
-
4JEE has native support for connection pools. What's your application server? – JB Nizet Sep 20 '12 at 10:18
-
1check out this http://stackoverflow.com/questions/2313197/jdbc-mysql-connection-pooling-practices – MaVRoSCy Sep 20 '12 at 10:32
-
@JBNizet Am using tomcat v7.0 – Raj Inevitable Sep 20 '12 at 10:39
-
2http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html – JB Nizet Sep 20 '12 at 10:42
1 Answers
6
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.

Matt Fellows
- 6,512
- 4
- 35
- 57