We have a Java web application that gets a connection from Hikari connection pool (ver. 3.2.0) in an HttpServlet as soon as it receives a request and closes it when the response is returned.
We set the leakDetectionThreshold
and the connectionTimeout
so we can identify the connections that leaks.
In "pseudocode" it looks like this:
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) {
try {
Connection connection = dataSource.getConnection();
String htmlResponse = doStuff(request, connection);
response.write(htmlResponse);
} catch (Exception exception) {
//handle exception
} finally {
connection.close();
}
}
private String doStuff(HttpServletRequest request, Connection connection) {
//...
}
}
In doStuff
we call an external service to get some data and sometimes it takes long to answer (>30 seconds), or we get no answer at all.
This (and the high traffic on the servlet) causes the pool to fill up and so we start getting the Connection leak detection
warning and once there are no more idle connections the SQLTransientConnectionException
is thrown with this message: Connection is not available, request timed out after 30000ms
.
This is the behaviour expected form Hikari and is totally fine.
My quesiton is: how can we handle this situation? Are there other solutions than just intercept the leaked connections and close them?