복붙노트

[SPRING] 밤새 연결이 끊어졌습니다 (spring boot + mysql).

SPRING

밤새 연결이 끊어졌습니다 (spring boot + mysql).

내 스프링 부팅 응용 프로그램은 항상 아침에이 화이트리스트 오류를 ​​표시합니다. 트랜잭션을 위해 JPA EntityManager를 열 수 없습니다. 중첩 예외는 javax.persistence.PersistenceException입니다 : org.hibernate.TransactionException : JDBC begin transaction failed :

나는 웹에서 mysql이 8 시간 동안 연결을 끊었다 고 생각했다. 그러나 문서에 따르면 스프링 부트는 자동으로 풀링 아파치 데이터 소스를 구성합니다. 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

데이터 소스의 경우 스프링 데이터 저장소를 사용합니다.

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

}

다음은 '% timeout %'와 같은 show variables를 실행중인 mysql 데이터베이스에 대한 결과입니다.

'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'

해결법

  1. ==============================

    1.이 답변을 확인하십시오 : Spring Boot JPA - 자동 재접속 구성

    이 답변을 확인하십시오 : Spring Boot JPA - 자동 재접속 구성

    요컨대, 당신은 필요합니다 :

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

    2.이 링크를 볼 수도 있습니다. application.properties 파일에 다음 행을 포함하라고 지시합니다.

    이 링크를 볼 수도 있습니다. application.properties 파일에 다음 행을 포함하라고 지시합니다.

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

    3.DB 서버에서 wait_timeout = 31536000 속성을 설정하여이 문제를 해결할 수 있습니다. 이것은 1 년과 같습니다. DB가 AWS 환경에서 실행중인 경우 매개 변수 그룹 -> 새 그룹 만들기 -> wait_timeout = 31536000을 설정하고 새로 만든 매개 변수 그룹을 RDS 인스턴스에 할당합니다. 이것을 봄 부팅 인스턴스에서 고치고 싶다면 C3P0 설정으로 만 수정할 수는 있지만 그 시도는하지 않았습니다.

    DB 서버에서 wait_timeout = 31536000 속성을 설정하여이 문제를 해결할 수 있습니다. 이것은 1 년과 같습니다. DB가 AWS 환경에서 실행중인 경우 매개 변수 그룹 -> 새 그룹 만들기 -> wait_timeout = 31536000을 설정하고 새로 만든 매개 변수 그룹을 RDS 인스턴스에 할당합니다. 이것을 봄 부팅 인스턴스에서 고치고 싶다면 C3P0 설정으로 만 수정할 수는 있지만 그 시도는하지 않았습니다.

  4. from https://stackoverflow.com/questions/31221877/connection-lost-overnight-spring-boot-mysql by cc-by-sa and MIT license