JDBCConnectionException:无法获取JDBC连接

时间:2020-05-22 20:23:01

标签: java spring-data-jpa hikaricp

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

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

下面是错误stackTrace

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引起此问题吗?

请给我建议。预先感谢

1 个答案:

答案 0 :(得分:1)

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

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

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

这一定是数据库池配置中的问题(错误的数据库URL?)。