【问题标题】:Connection lost overnight (spring boot + mysql)一夜之间失去连接(spring boot + mysql)
【发布时间】:2015-09-22 04:26:21
【问题描述】:

我的 Spring Boot 应用程序总是在早上向我显示这个白标签错误:无法打开 JPA EntityManager 进行事务处理;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin transaction failed:

我在网上搜索了我认为可能是 mysql 关闭连接 8 小时不活动。但是根据文档,spring boot 会自动配置一个池化 apache 数据源。 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

我不确定如何配置应用程序代码或数据库。

这是我正在使用的 build.gradle:

buildscript {
    ext {
        springBootVersion = '1.2.3.RELEASE'
    }
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'application'

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-snapshot" }
    maven { url "http://maven.springframework.org/milestone" }
}

// Seems tomcat 8 doesn't work with paypal
configurations.all {
    resolutionStrategy {
        eachDependency {
            if (it.requested.group == 'org.apache.tomcat.embed') {
                it.useVersion '7.0.59'
            }
        }
    }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-tomcat:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-mail:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-aop:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
    compile("org.springframework.data:spring-data-rest-webmvc")
    compile("javax.servlet:jstl:1.2")
    compile("org.apache.logging.log4j:log4j-api:2.3")
    compile("org.apache.logging.log4j:log4j-core:2.3")
    compile("com.paypal.sdk:rest-api-sdk:1.2.1")
    compile("com.opencsv:opencsv:3.4")
    compile("mysql:mysql-connector-java:5.1.35")
    compile("com.google.guava:guava:17.0")
    compile("org.apache.httpcomponents:httpclient:4.3.4")
    compile("com.squareup.retrofit:retrofit:1.6.0")
    compile("commons-io:commons-io:2.4")
    compile("org.apache.commons:commons-lang3:3.4")
    compile("com.amazonaws:aws-java-sdk:1.9.34")

    providedCompile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.22")

    testCompile("junit:junit")
}

sourceCompatibility = 1.7
targetCompatibility = 1.7

war {
    baseName = 'gs-convert-jar-to-war'
    version =  '0.1.0'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

这是application.properties中的数据库配置:

spring.datasource.url=jdbc:mysql://fakeurl:3306/qa
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
#
# hibernate
#
spring.jpa.hibernate.ddl-auto=update

对于数据源,我使用 spring data 的存储库:

@Repository
public interface EventRepository extends CrudRepository<EventDetail, Long> {

}

这是针对 mysql 数据库运行“show variables like '%timeout%':”的结果:

'connect_timeout','10'
'delayed_insert_timeout','300'
'innodb_flush_log_at_timeout','1'
'innodb_lock_wait_timeout','50'
'innodb_rollback_on_timeout','OFF'
'interactive_timeout','28800'
'lock_wait_timeout','31536000'
'net_read_timeout','30'
'net_write_timeout','60'
'rpl_stop_slave_timeout','31536000'
'slave_net_timeout','3600'
'wait_timeout','28800'

【问题讨论】:

    标签: mysql spring hibernate spring-boot


    【解决方案1】:

    您可以通过在数据库服务器中设置属性 wait_timeout = 31536000 来解决此问题。这相当于一年。 如果您的数据库在 AWS 环境中运行,请转到参数组-> 创建一个新组-> 设置 wait_timeout = 31536000 并将新创建的参数组分配给您的 RDS 实例。 如果你想在 spring boot 实例中修复这个问题,你可以只通过 C3P0 设置来修复,但我没有尝试过。

    【讨论】:

      【解决方案2】:

      你也可以看到这个link 它告诉您在 application.properties 文件中包含以下行。

      spring.datasource.testWhileIdle = true
      spring.datasource.timeBetweenEvictionRunsMillis = 3600000
      spring.datasource.validationQuery = SELECT 1
      

      【讨论】:

        【解决方案3】:

        检查这个答案:Spring Boot JPA - configuring auto reconnect

        简而言之,您需要:

        spring.datasource.testOnBorrow=true
        spring.datasource.validationQuery=SELECT 1
        

        【讨论】:

        • 最近两天没看到这条消息。我认为它正在工作,
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 2017-11-13
        • 2017-12-31
        • 1970-01-01
        • 2019-10-20
        • 2018-03-22
        • 2021-03-20
        相关资源
        最近更新 更多