HikariPool-1 - 连接不可用,请求超时

Posted

技术标签:

【中文标题】HikariPool-1 - 连接不可用,请求超时【英文标题】:HikariPool-1 - Connection is not available, request timed out after 【发布时间】:2016-12-05 22:08:22 【问题描述】:

我使用 HikariCP 2.4.7 作为连接池。启动应用程序后一切都很好,但一段时间后没有调用getConnection(),当我尝试getConnection()时出现此错误:

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 42734ms.
    at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:555)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:188)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:147)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:99)
    at pl.airly.database.ComplexOperations.getSensorDataFromArea(ComplexOperations.java:310)
    at pl.airly.database.ComplexOperations.getMeasurementsInAreaWithinTimeFrame(ComplexOperations.java:201)
    at pl.airly.ResponseGenerator.getResponseAdequateToRequest(ResponseGenerator.java:139)
    at sun.reflect.GeneratedMethodAccessor24.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

在启动期间我正在初始化HikariDataSource:

hikariDataSource = new HikariDataSource();
hikariDataSource.setDriverClassName("org.postgresql.Driver");
hikariDataSource.setUsername(DATABASE_USER_NAME);
hikariDataSource.setPassword(DATABASE_PASSWORD);
hikariDataSource.setJdbcUrl(DATABASE_URL);
hikariDataSource.setLeakDetectionThreshold(5000);

然后我就这样使用它:

try(java.sql.Connection conn = Connection.hikariDataSource.getConnection()) 
            Record1 record1 = DSL.using(conn).select(SENSOR.ID) //jOOQ
                    .from(SENSOR)
                    .where(SENSOR.ID.equal(1))
                    .limit(1)
                    .fetchOne();
            Debug.println(record1.getValue(0).toString());
         catch (SQLException e) 
            e.printStackTrace();
        

我做错了什么?如何解决此问题?

编辑:

添加这个可以解决问题:

hikariDataSource.setIdleTimeout(60000);
hikariDataSource.setConnectionTimeout(60000);
hikariDataSource.setValidationTimeout(3000);
hikariDataSource.setLoginTimeout(5);
hikariDataSource.setMaxLifetime(60000);

这可能与在 HikariCP 之前关闭连接的服务器有关,但我不确定。对此的任何评论将不胜感激。

【问题讨论】:

您是否能够调试并缩小问题范围? 很遗憾,没有,我想这可能是服务器在 HikariCP 之前关闭了连接,但是,在我遇到这个问题后不久,我决定以另一种方式处理连接。 您正在使用连接池 (HikariPool) 但使用“try(java.sql.Connection conn = Connection.hikariDataSource.getConnection())”(try-with-resources 语句确保每个资源在语句末尾关闭)您在“Debug.println”之后关闭连接。也许这可能是个问题。 无法删除我上面的​​评论 - 我以为您正在使用关闭连接的 try-with-resources 语句,但如果您调用“关闭”,Hikari 实现只会将连接释放回池中。 【参考方案1】:

我先说我不是 Java 程序员,你也可以尝试关闭 jOOQ DSL:

try(java.sql.Connection conn = Connection.hikariDataSource.getConnection()) 
        try (Record1 record1 = DSL.using(conn).select(SENSOR.ID)) 
                .from(SENSOR)
                .where(SENSOR.ID.equal(1))
                .limit(1)
                .fetchOne();
        Debug.println(record1.getValue(0).toString());
     catch (Exception e) 
        e.printStackTrace();
       
 catch (SQLException e) 
    e.printStackTrace();

【讨论】:

他正在使用 try-with-resources 自动关闭任何 AutoClosable

以上是关于HikariPool-1 - 连接不可用,请求超时的主要内容,如果未能解决你的问题,请参考以下文章

HikariPool-1 - Connection is not available, request timed out after XXXXms.

nas请求网络超时,错误2001

HikariPool连接池配置

HikariPool-1 - 无法验证连接 org.postgresql.jdbc.PgConnection@2a84e649(此连接已关闭。)

go-zero高可用-自适应熔断器

如何解决高并发,连接等待超时的异常