Two important things to check in this kind of problems:
Read the server logs for any exceptions and stacktraces. In Tomcat it's located in /logs
folder. If you found any, then those should provide sufficient information to fix the problem yourself. But if you don't understand the cause, then you need to update your question to include the relevant exceptions and stacktraces. This way we can assist in explaining the cause so that the solution is obvious enough.
Ensure that the code is not swallowing important exceptions anywhere. I.e. you need to at least rethrow or log them after catching. E.g. thus not:
try {
// ...
} catch (SQLException e) {
}
But more so:
try {
// ...
} catch (SQLException e) {
throw new ServletException(e);
}
Or, at least:
try {
// ...
} catch (SQLException e) {
e.printStackTrace();
}
This way they will be logged to the server logs.
One of first things which comes to mind in your particular case is that the code is not closing database resources properly in finally
block and the DB thus runs out of resources which leads to a failure in connecting, preparing statement and/or executing the query. With proper exception handling this should already be hinted in the server logs. Here's a basic kickoff example how to write JDBC code the proper way. The normal practice is to acquire and close the database resources in shortest possible scope:
public List<Entity> list() throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<Entity> entities = new ArrayList<Data>();
try {
connection = database.getConnection();
statement = connection.createStatement("SELECT id, name, value FROM entity");
resultSet = statement.executeQuery();
while (resultSet.next()) {
Entity entity = new Entity();
entity.setId(resultSet.getLong("id"));
entity.setName(resultSet.getString("name"));
entity.setValue(resultSet.getInteger("value"));
entities.add(entity);
}
} finally {
// Close in reversed order.
if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return entities;
}
Any exception on close()
can be ignored, but I prefer to log them so that you don't lost any evidence of a failure anywhere, even if it's minor.