DB connection pooling JSPs/servlets Surfing the internet on the topic of database connection pooling left me a little bit confused, because there are two different approaches. There is a whole chapter in the O'reilly book "Java Server Pages" which clarifies this nicely (get it from safari.com)
Here's a sumary:
You can either use a
JDBC 2 DataSource object, which means the webserver manages the connections, or you can have a
java class which manages connections. JDBC 2 Datasource approach should be your first choice. It involves putting a <resource> tag somewhere in your web server configuration. This is web server specific, and is not supported by all web-servers. I am having to use the static class approach. I am using DbConnectionBroker from
www.javaexchange.com There's a different one from here here:
http://www.webdevelopersjournal.com/columns/connection_pool.html Either way
you should construct the manager/broker class in your servlet/JSP init method.
For the connection itself, you should always get --> use --> release a connection for every request (i.e. in the 'doPost' method). Don't get a connection in the init method, and Dont keep a connection as a session variable, or in a bean. It might seem like a good idea, but it isn't.