Hikari CP连接错误

时间:2017-12-19 14:49:24

标签: mysql jdbc spring-data connection-pooling hikaricp

我们有一个位置服务应用程序,它从MySQL数据库向我们的其他应用程序提供位置相关信息。由于某种原因,位置服务在接收到多个请求时无法连接到MySQL数据库。我们观察到它在同时获得连续15-20请求时失败。以下代码段是MySQL配置代码和错误堆栈。

 My@Bean(name = "locationDataSource")
public HikariDataSource getDataSource(@Value("${projectxx.mysql.location.url}") String url,
        @Value("${projectxx.mysql.location.username}") String username,
        @Value("${projectxx.mysql.location.password}") String password) {


    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(url);
    config.setUsername(username);
    config.setPassword(password);
    config.setDriverClassName("com.mysql.jdbc.Driver");
    config.setMaximumPoolSize(45);
    config.setPoolName("locationDBpool");
    config.setMinimumIdle(15);

    config.addDataSourceProperty("cachePrepStmts", "true");
    config.addDataSourceProperty("prepStmtCacheSize", "250");
    config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

    return new HikariDataSource(config);

}

@Bean(name = "locationSession")
@Qualifier("locationDataSource")
public SessionFactory getSessionFactoryBean(@Autowired(required = true) HikariDataSource dataSource) {

    LocalSessionFactoryBean sessionFacBean = new LocalSessionFactoryBean();
    sessionFacBean.setDataSource(dataSource);
    sessionFacBean.setPackagesToScan("com.projectxxx.dao");
    Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    sessionFacBean.setHibernateProperties(hibernateProperties);
    try {
        sessionFacBean.afterPropertiesSet();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sessionFacBean.getObject();

}
  

java.sql.SQLTransientConnectionException:locationDBpool - 连接不可用,请求在30000ms后超时。       在com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:666)〜[HikariCP-2.7.4.jar:na]       在com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:182)〜[HikariCP-2.7.4.jar:na]       在com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:147)〜[HikariCP-2.7.4.jar:na]       在com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:85)〜[HikariCP-2.7.4.jar:na]       at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final]       at org.hibernate.internal.AbstractSessionImpl $ NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:386)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]       at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:87)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]       at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:112)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final]       at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:230)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final]       在org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:237)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]       在org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl $ TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214)〜[休眠核-5.0.12.Final.jar:5.0.12.Final]       在org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]       在org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1512)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]       在com.projectxxx.actionImpl.LocationManagementImpl.findAddressByPostalCode(LocationManagementImpl.java:40)〜[classes /:na]       在com.projectxxx.controller.LocationReadController.service3(LocationReadController.java:40)〜[classes /:na]       at sun.reflect.GeneratedMethodAccessor51.invoke(Unknown Source)〜[na:na]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_131]       在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_131]       在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)〜[弹簧webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)〜[弹簧webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)~ [spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在javax.servlet.http.HttpServlet.service(HttpServlet.java:635)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)〜[tomcat-embed-websocket-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)〜[tomcat-embed-core-8.5.23.jar:8.5.23]       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)[tomcat-embed-core-8.5.23.jar:8.5.23]       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)[tomcat-embed-core-8.5.23.jar:8.5.23]       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)[tomcat-embed-core-8.5.23.jar:8.5.23]       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)[tomcat-embed-core-8.5.23.jar:8.5.23]       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)[tomcat-embed-core-8.5.23.jar:8.5.23]       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)[tomcat-embed-core-8.5.23.jar:8.5.23]       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[na:1.8.0_131]       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)[na:1.8.0_131]       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-8.5.23.jar:8.5.23]       在java.lang.Thread.run(Thread.java:748)[na:1.8.0_131]

1 个答案:

答案 0 :(得分:0)

您将最大池大小设置为45,这意味着如果连接了45个连接,则下一个请求将等待一个释放,在您的情况下请求等待,直到30秒后超时发生。

如果您有15-20个请求,可能每个请求至少需要3个连接,并且不会在30秒内释放它们。

可能存在连接泄漏,这意味着您打开了一个连接但未关闭它会导致连接在池中保持打开状态。

如果没有泄漏,您的SQL可能需要很长时间才能提高性能。

您应该监控您的环境并找到瓶颈/泄漏。