当前位置: 首页 > 知识库问答 >
问题:

一夜之间连接丢失(spring boot+mysql)

尚安平
2023-03-14

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

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

}

下面是运行“show variables like'%timeout%'”的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个答案

段阳夏
2023-03-14

检查以下答案:Spring Boot JPA-配置自动重新连接

简而言之,您需要:

spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
 类似资料:
  • 问题内容: 我们正在使用 订阅主题。但是,我们经常遇到错误。我已经使用JavaScript客户端(mqttws31.js)测试了连接,效果很好。表示连接没有问题。 错误:- MQTT消息:- 配置 :- pom.xml: 在调试时:- 在上述方法中,有时会抛出。所以基于catch块,它使用 问题答案: 我只是想分享一下,以防万一。…我有相同的异常,并通过确保生成了唯一的客户端ID(使用 )来解决它

  • 问题内容: 在生产中,我有一个使用连接局部变量保存游戏状态的游戏。但是,我注意到,如果我在连接上闲置了一段时间,它将断开连接并重新连接,这将丢失当前状态。在本地主机上进行测试时,我从未注意到此行为。这是套接字连接的规范行为,还是导致连接断开的其他原因。 如果是正常行为,通常如何处理?是否应该将连接值全局存储,以便用户断开/重新连接时可以恢复连接值? 问题答案: 您的问题与套接字超时有关。如果某个套

  • 我试图在我的设备(华为Nova 4)上运行我的应用程序,但它崩溃了,在另一部手机上却没有。 颤振问题,救命

  • 自上次更新以来,启动应用程序后,“Project Explorer”中的FTP-Server-Connections丢失。通常我可以很容易地选择服务器和浏览文件,但现在一秒钟后,所有的列表条目都丢失了,它只显示“本地文件系统”了。 (之前在Local Filesystem下列出的是我的服务器:http://d.pr/I/zbc1)昨天我重新添加了它,我以为它可以工作,但今天它又丢失了... 编辑:

  • 我正在尝试从macos上使用swift开发的应用程序连接到realm object server 当我尝试连接时,会出现“网络连接丢失”错误,但我可以从浏览器中毫无问题地打开领域服务器。。顺便说一句,这个问题最近发生了,它在几周前工作正常 这里,用户总是,错误描述是 这发生在本地服务器和一个托管在数字海洋 服务器版本1.8。3. xcode控制台日志 2018-01-15 12:46:07.077

  • php STOMP连接和运行在不同服务器上的ActiveMQ代理之间的通信出现问题。布局如下; 服务器B有一个AMQ代理,运行以下transportConnector设置; 服务器B还有一个Java客户机,充当许多队列的使用者。 问题是,当我们在ServerA上运行脚本时,它会失败,出现以下错误; 致命错误:未捕获的异常“stompexception”,消息为“服务器没有响应” 在ServerB上