Retrieve a connection, run the filter chain, and return the connection.
Connection con = null;
try {
con = dataSource.getConnection();
// Set the connection, and retrieve the previous connection for
// disposal
Connection previousCon = ConnectionManager.setConnection(con);
if(previousCon != null)
try { previousCon.close(); } catch (SQLException e) {}
// Run the rest of the filter chain.
chain.doFilter(request, response);
// Make sure we disassociate the connection, just in case.
ConnectionManager.setConnection(null);
} catch (SQLException e) {
ServletException se = new ServletException(e);
throw se;
} finally {
if (con != null)
try { con.close(); } catch (SQLException e) {}
}