zoukankan      html  css  js  c++  java
  • spring使用mysql出现连接不可用,请求超时

    ################

    业务报错日志:只需看caused by部分:

    org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
            at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:446)
            at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:378)
            at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:474)
            at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:289)
            at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
            at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
            at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
            at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
            at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
            at com.sun.proxy.$Proxy123.save(Unknown Source)
            at com.xiaomi.infra.yarn.rest.consumer.SyncAppConsumer$MyMessageProcessor.parseAndInsertIntoApps(SyncAppConsumer.java:158)
            at com.xiaomi.infra.yarn.rest.consumer.SyncAppConsumer$MyMessageProcessor.process(SyncAppConsumer.java:90)
            at com.xiaomi.infra.galaxy.talos.consumer.TalosMessageReader.fetchData(TalosMessageReader.java:118)
            at com.xiaomi.infra.galaxy.talos.consumer.PartitionFetcher$FetcherStateMachine.run(PartitionFetcher.java:96)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
            at java.lang.Thread.run(Thread.java:748)
    Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
            at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48)
            at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
            at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:109)
            at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:136)
            at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:254)
            at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:262)
            at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:236)
            at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:80)
            at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:183)
            at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:401)
            ... 24 common frames omitted
    Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30580ms.
            at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:676)
            at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:190)
            at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:155)
            at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128)
            at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)

    根据报错日志可得:连接不可用,请求超时。

    业务反馈插入数据不成功后,然后找出了上面的报错日志甩给我。

    业务使用的是spring的默认配置:

    pring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
        spring.datasource.url=jdbc:mysql://ip:port/db?autoReconnect=true&characterEncoding=utf-8
        spring.datasource.username=xxx
        spring.datasource.password=yyy
        spring.jpa.show-sql=false
        spring.jpa.hibernate.ddl-auto=none
        spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
        spring.jackson.serialization.indent_output=true

    问题原因:通过异常可以发现无法获取数据库连接。估计是连接数不够。因此,修改HikariPool连接池配置即可解决问题。

                         该错误的本质原因其实就是线程去borrow连接超时抛出的,超时的原因说白了就是超时时间内一直拿不到可用连接。

    解决方案:加大连接池大小,加大连接请求超时时间

    spring:
      datasource:
        hikari:
          connection-test-query: SELECT 1 FROM DUAL
          connection-timeout: 600000
          maximum-pool-size: 500
          max-lifetime: 1800000
          minimum-idle: 20
          validation-timeout: 3000
          idle-timeout: 60000
          connection-init-sql: SET NAMES utf8mb4

    ###################

    igoodful@qq.com
  • 相关阅读:
    iOS开发之JSONKit
    iOS开发之XMPPFramework环境搭建和配置
    iOS开发之UICollectionViewController
    iOS开发之UITableViewController重写
    Object-c 开发之property
    Git使用
    解决CocoaPods下载慢的问题
    解决Linux下编译adb和fastboot问题
    解决fastboot刷机内存消耗问题
    sql
  • 原文地址:https://www.cnblogs.com/igoodful/p/15329732.html
Copyright © 2011-2022 走看看