连接不可用,请求在30003ms之后超时

时间:2018-05-15 01:19:33

标签: java connection connection-pooling hikaricp

过去几天我得到了这个例外。

当我处理10 ++表单并且连接每30秒增加到1分30秒直到达到最大大小时,会出现此问题。

我确保所有连接都已关闭。 为什么我一直收到这个错误?

环境:

  • HikariCP版本:2.5.1
  • JDK版本:1.8.0_152
  • 数据库:MySQL

我继续收到此错误:

Caused by: javax.servlet.ServletException: java.sql.SQLTransientConnectionException: ConcolePool - Connection is not available, request timed out after 30003ms.
	at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
	at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
	at org.apache.jsp.utilBillParaList_jsp._jspService(utilBillParaList_jsp.java:761)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
	... 51 more
Caused by: java.sql.SQLTransientConnectionException: ConcolePool - Connection is not available, request timed out after 30003ms.
	at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:548)
	at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:186)
	at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:145)
	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:83)
	at org.apache.jsp.utilBillParaList_jsp._jspService(utilBillParaList_jsp.java:322)

配置:

09:05:37,908 DEBUG com.zaxxer.hikari.HikariConfig :841 - ConcolePool - configuration:
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - allowPoolSuspension.............false
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - autoCommit......................true
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - catalog.........................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - connectionInitSql...............null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - connectionTestQuery............."SELECT current_timestamp"
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - connectionTimeout...............30000
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - dataSource......................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - dataSourceClassName............."com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - dataSourceJNDI..................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - dataSourceProperties............{port=3307, user=root, password=<masked>, prepStmtCacheSqlLimit=2048, cachePrepStmts=true, prepStmtCacheSize=500, serverName=localhost}
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - driverClassName.................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - healthCheckProperties...........{}
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - healthCheckRegistry.............null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - idleTimeout.....................600000
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - initializationFailFast..........true
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - isolateInternalQueries..........false
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - jdbc4ConnectionTest.............false
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - jdbcUrl.........................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - leakDetectionThreshold..........30000
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - maxLifetime.....................1800000
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - maximumPoolSize.................10
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - metricRegistry..................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - metricsTrackerFactory...........null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - minimumIdle.....................5
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - password........................<masked>
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - poolName........................"ConcolePool"
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - readOnly........................false
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - registerMbeans..................false
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - scheduledExecutorService........null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - threadFactory...................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - transactionIsolation............null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - username........................null
09:05:37,924 DEBUG com.zaxxer.hikari.HikariConfig :857 - validationTimeout...............5000
09:05:37,924  INFO com.zaxxer.hikari.HikariDataSource :70 - ConcolePool - Started.

调试池状态:

2018-05-14 12:48:19  [ ConcolePool connection adder:5403109 ] - [ DEBUG ]  ConcolePool - After adding stats (total=5, active=0, idle=5, waiting=0)
2018-05-14 12:48:49  [ ConcolePool housekeeper:5433109 ] - [ DEBUG ]  ConcolePool - Pool stats (total=5, active=1, idle=4, waiting=0)
2018-05-14 12:48:49  [ ConcolePool connection adder:5433109 ] - [ DEBUG ]  ConcolePool - Added connection com.mysql.jdbc.JDBC4Connection@39193bcd
2018-05-14 12:48:49  [ ConcolePool connection adder:5433109 ] - [ DEBUG ]  ConcolePool - After adding stats (total=6, active=1, idle=5, waiting=0)
2018-05-14 12:49:07  [ ConcolePool housekeeper:5450750 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@160af66e, stack trace follows
2018-05-14 12:49:19  [ ConcolePool housekeeper:5463125 ] - [ DEBUG ]  ConcolePool - Pool stats (total=6, active=1, idle=5, waiting=0)
2018-05-14 12:49:49  [ ConcolePool housekeeper:5493140 ] - [ DEBUG ]  ConcolePool - Pool stats (total=6, active=2, idle=4, waiting=0)
2018-05-14 12:49:49  [ ConcolePool connection adder:5493140 ] - [ DEBUG ]  ConcolePool - Added connection com.mysql.jdbc.JDBC4Connection@207b7ac0
2018-05-14 12:49:49  [ ConcolePool connection adder:5493140 ] - [ DEBUG ]  ConcolePool - After adding stats (total=7, active=2, idle=5, waiting=0)
2018-05-14 12:49:54  [ ConcolePool housekeeper:5497687 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@10429350, stack trace follows
2018-05-14 12:50:19  [ ConcolePool housekeeper:5523156 ] - [ DEBUG ]  ConcolePool - Pool stats (total=7, active=2, idle=5, waiting=0)
2018-05-14 12:50:49  [ ConcolePool housekeeper:5553172 ] - [ DEBUG ]  ConcolePool - Pool stats (total=7, active=3, idle=4, waiting=0)
2018-05-14 12:50:49  [ ConcolePool connection adder:5553172 ] - [ DEBUG ]  ConcolePool - Added connection com.mysql.jdbc.JDBC4Connection@4721d5d3
2018-05-14 12:50:49  [ ConcolePool connection adder:5553172 ] - [ DEBUG ]  ConcolePool - After adding stats (total=8, active=3, idle=5, waiting=0)
2018-05-14 12:50:58  [ ConcolePool housekeeper:5561656 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@76ff6438, stack trace follows
2018-05-14 12:51:19  [ ConcolePool housekeeper:5583187 ] - [ DEBUG ]  ConcolePool - Pool stats (total=8, active=4, idle=4, waiting=0)
2018-05-14 12:51:19  [ ConcolePool connection adder:5583187 ] - [ DEBUG ]  ConcolePool - Added connection com.mysql.jdbc.JDBC4Connection@1f4c60a8
2018-05-14 12:51:19  [ ConcolePool connection adder:5583187 ] - [ DEBUG ]  ConcolePool - After adding stats (total=9, active=4, idle=5, waiting=0)
2018-05-14 12:51:42  [ ConcolePool housekeeper:5606359 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@1cff4d1c, stack trace follows
2018-05-14 12:51:49  [ ConcolePool housekeeper:5613203 ] - [ DEBUG ]  ConcolePool - Pool stats (total=9, active=4, idle=5, waiting=0)
2018-05-14 12:52:19  [ ConcolePool housekeeper:5643219 ] - [ DEBUG ]  ConcolePool - Pool stats (total=9, active=4, idle=5, waiting=0)
2018-05-14 12:52:49  [ ConcolePool housekeeper:5673234 ] - [ DEBUG ]  ConcolePool - Pool stats (total=9, active=5, idle=4, waiting=0)
2018-05-14 12:52:49  [ ConcolePool connection adder:5673234 ] - [ DEBUG ]  ConcolePool - Added connection com.mysql.jdbc.JDBC4Connection@2d750b07
2018-05-14 12:52:49  [ ConcolePool connection adder:5673234 ] - [ DEBUG ]  ConcolePool - After adding stats (total=10, active=5, idle=5, waiting=0)
2018-05-14 12:52:53  [ ConcolePool housekeeper:5676453 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@708cdc47, stack trace follows
2018-05-14 12:53:19  [ ConcolePool housekeeper:5703250 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=6, idle=4, waiting=0)
2018-05-14 12:53:43  [ ConcolePool housekeeper:5726594 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@39193bcd, stack trace follows
2018-05-14 12:53:49  [ ConcolePool housekeeper:5733265 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=6, idle=4, waiting=0)
2018-05-14 12:54:19  [ ConcolePool housekeeper:5763281 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=7, idle=3, waiting=0)
2018-05-14 12:54:39  [ ConcolePool housekeeper:5782984 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@207b7ac0, stack trace follows
2018-05-14 12:54:49  [ ConcolePool housekeeper:5793297 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=7, idle=3, waiting=0)
2018-05-14 12:55:19  [ ConcolePool housekeeper:5823312 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:55:45  [ ConcolePool housekeeper:5848906 ] - [ WARN ]  Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@4721d5d3, stack trace follows
2018-05-14 12:55:49  [ ConcolePool housekeeper:5853328 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:56:19  [ ConcolePool housekeeper:5883344 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:56:49  [ ConcolePool housekeeper:5913359 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:57:20  [ ConcolePool housekeeper:5943375 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:57:50  [ ConcolePool housekeeper:5973390 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:58:20  [ ConcolePool housekeeper:6003406 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:58:50  [ ConcolePool housekeeper:6033422 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=8, idle=2, waiting=0)
2018-05-14 12:59:20  [ ConcolePool housekeeper:6063437 ] - [ DEBUG ]  ConcolePool - Pool stats (total=10, active=10, idle=0, waiting=1)

如何解决此问题?有什么我需要改变的吗?

修改

public Connection getConnection(){
    try{
        return datas.getConnection();

    } catch (SQLException e){
        e.printStackTrace();
        datas.resumePool();
        return null;
    }
}

关闭连接示例:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    DataSource datas = (DataSource) getServletContext().getAttribute("ds");
    Connection conn = null;
    Statement stmt = null;

    try{
        conn = datas.getConnection();
        stmt = conn.createStatement();

        //some sql
        //process form
        //pass the return values

        stmt.close();
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:3)

您没有始终关闭连接,因此连接池达到了极限。

必须关闭完成阻止时的连接和语句(作为所有资源),如果发生异常,则采用try with resources方法的正确/简单方法:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

  DataSource datas = (DataSource) getServletContext().getAttribute("ds");

  try (Connection conn = datas.getConnection();Statement stmt = conn.createStatement()){


    //some sql
    //process form
    //pass the return values

  } catch (Exception e) {
    e.printStackTrace();
  }
}
  

try-with-resources声明   try-with-resources语句是一个声明一个或多个资源的try语句。资源是在程序完成后必须关闭的对象。 try-with-resources语句确保在语句结束时关闭每个资源。