I have a java programs that gives me an error after working fine for a few hours ...
IOException: No buffer space available (maximum connections reached?)
The JDBC code is likely not properly closing connections in the finally
block of the try
where the connection is been acquired. This way the connections will be kept open until the DB forcibly times-out and closes them. The timeout depends on the DB config used. Apparently the timeout was been relatively short in the previous machine and relatively long in the new machine. When the DB runs out of available connections because your application never closes them, then you will get an exception like that.
The following code examples illustrates the normal (basic) JDBC idiom for resource handling (note the code flow and the code comments):
public List<Entity> list() throws SQLException {
// Declare resources.
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Entity> entities = new ArrayList<Entity>();
try {
// Acquire resources.
connection = database.getConnection();
statement = connection.prepareStatement("SELECT id, name, value FROM entity");
resultSet = statement.executeQuery();
// Gather data.
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 resources 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 data.
return entities;
}