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?)
的JDBC代碼很可能在其中已經獲取該連接的try
的finally
塊未正確關閉連接。這樣,連接將保持打開,直到數據庫強制超時並關閉它們。超時取決於使用的數據庫配置。顯然,在以前的機器中超時時間相對較短,而在新機器中超時時間相對較長。當數據庫用完可用連接,因爲你的應用程序永遠不會關閉它們,那麼你將會得到這樣的異常。
下面的代碼示例說明正常(基本)JDBC資源處理成語(注碼流和代碼註釋):
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;
}
還沒有得到確切的解決方案,但我也認爲它是與套接字有關...註冊表解決方案太冒險,所以雲沒有嘗試它..並沒有得到確切的方式做到這一點。 – 2011-01-25 07:46:45