【问题标题】:JDBCConnectionException: Unable to acquire JDBC ConnectionJDBCConnectionException:无法获取 JDBC 连接
【发布时间】:2020-05-22 20:23:01
【问题描述】:

我看过很多关于此类问题的帖子,但我还没有找到合适的解决方案。

我有一个 Spring Boot 应用程序,每天定期从其他来源调用它。最近我们看到 JDBCConnectionException

下面是错误堆栈跟踪

WARN o.h.e.jdbc.spi.SqlExceptionHelper@logExceptions:137 - SQL Error: 0, SQLState: null
ERROR o.h.e.jdbc.spi.SqlExceptionHelper@logExceptions:142 - HikariPool-1 - Connection is not available, request timed out after 928162ms.
ERROR c.w.locationmaster.service.DBService@getAllCustomers:110 - 
Exception in getAllCustomers(): Exception org.springframework.dao.DataAccessResourceFailureException: Unable to acquire JDBC Connection; 
nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection, stackTrace 
[org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:275), 
org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253), 
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527), 
org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61), 
org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242), 
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke

我用

Springboot 2.1.6
gradle-5.4.1

分级:

plugins {
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
apply plugin: 'jdepend'

sourceCompatibility = 1.11

bootJar {
    baseName = 'customers-service'
}

checkstyle {
    ignoreFailures = true
    toolVersion = '8.2'
    configDir = file("$rootProject.projectDir/etc/checkstyle")
    System.setProperty('checkstyle.cache.file', String.format('%s/%s', buildDir, 'checkstyle.cachefile'))
}

repositories {
    mavenCentral()
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter')
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-cache')
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-mail'
    compile 'org.springframework.boot:spring-boot-starter-web'

    compile('org.springframework.boot:spring-boot-starter-jdbc') {
        exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
    }
    compile('com.fasterxml.jackson.core:jackson-annotations:2.9.4')
    compile('com.fasterxml.jackson.core:jackson-databind:2.9.4')

    compile('org.apache.httpcomponents:httpclient:4.3.4')

    compile('org.apache.commons:commons-lang3:3.5')
    compile('org.apache.commons:commons-collections4:4.0')

    testCompile('org.springframework.boot:spring-boot-starter-test')
    runtime('com.microsoft:sqljdbc4:4.0') {
        exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
    }
}

application.properties

server.servlet.context-path=/customers
server.port=XXXX

spring.security.user.name=XXXX
spring.security.user.password=XXXX

management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
management.health.ldap.enabled=false
management.endpoints.web.cors.allow-credentials=true

spring.datasource.url=jdbc:sqlserver://XXXX;databaseName=XXX
spring.datasource.username=XXXX
spring.datasource.password=XXXX
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.platform=sqlserver

spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2014Dialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

spring.datasource.validation-query=select 1
spring.datasource.testOnBorrow=true

我有 @Repository 类和一个带有

的方法
@Transactional(readOnly = true) 

要从数据库中检索记录,@Transactional 是否会导致问题?

请给我建议。提前致谢

【问题讨论】:

  • 是@Transactional 导致了问题吗? 不是,系统花费了 928!几秒钟试图建立连接,然后超时。您应该设置一个较低的超时阈值,并修复您的配置。它出什么问题了?我不知道。我不知道你的数据库是如何配置的。但我知道你的配置不正确。
  • @ElliottFrisch:我需要使用 spring.datasource.jdbc-url 而不是 spring.datasource.url 吗?还没有尝试过,但我记得我在其他项目中与 springboot 2 的 db 连接遇到了一些问题,并且解决了这个问题。
  • doubt it.

标签: java spring-data-jpa hikaricp


【解决方案1】:

这不是@Transactional,它甚至没有达到这一点。

您的应用程序无法建立数据库连接:

HikariPool-1 - Connection is not available, request timed out

一定是DB池配置有问题(DB url错误?)。

【讨论】:

  • 我只是参考了其他项目,并注意到我提到的 url 为 spring.datasource.url=jdbc:sqlserver://customerqa.company.com;databaseName=XXX(完全限定名称)但在这里我指定为 'customerqa' spring.datasource.url=jdbc:sqlserver://customerqa;databaseName=XXX 。它一直在工作,但不确定这可能是原因吗?
猜你喜欢
  • 1970-01-01
  • 2017-11-09
  • 2017-08-02
  • 2019-01-10
  • 1970-01-01
  • 2017-09-03
  • 2019-04-18
  • 2020-11-06
  • 1970-01-01
相关资源
最近更新 更多