Datasource in jdbc connection pooling not returning connection [duplicate]

牧云@^-^@ 提交于 2019-12-02 10:17:11

You forgot to close the connection

Use a finally statement to close a pooled connection. The following finally block would appear after the try/catch block that applies to the code in which the pooled connection was used:

try {
     Connection con = 
  ds.getConnection(username, password);
     // ... code to use the pooled
     // connection con
 } catch (Exception ex {
     // ... code to handle exceptions
 } finally {
     if (con != null) con.close();
 }

Your datasource reached the maximum connection defined.

Your code seems to be one big race condition. Only one servlet instance is used for multiple requests. As a result, on concurrent requests, your current code can and will leak connections.

When concurrent requests are executed, each of them will create a connection and assign it to the same instance variable, so one or more connections will be lost and remain open. The use of that DBConnection.getConnection/DBConnection.closeConnection suggests that you are potentially leaking connections there as well.

Please stop using fields to keep your connection and statement, and make these local variables instead. Also consider using try-with-resources to properly close connections, and consider using a DataSource directly instead of using that (probably unnecessary) abstraction of DBConnection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!