当前位置: 首页 > 面试题库 >

失去连接(Spring Boot + MySQL)

季阳朔
2023-03-14
问题内容

我的Spring Boot应用程序总是在早上向我显示此whitelabel错误:无法打开JPA
EntityManager进行事务;嵌套异常是javax.persistence.PersistenceException:org.hibernate.TransactionException:JDBC开始事务失败:

我在网上搜索,我认为可能是mysql关闭了8个小时的非活动连接。但是,根据该文档,spring boot将自动配置pooling
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数据的存储库:

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

}

这是针对运行“显示变量,如’%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'

问题答案:

检查这个答案:Spring Boot JPA-配置自动重新连接
简而言之,您需要:

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


 类似资料:
  • STOMP 一种简单的消息传递协议,最初创建用于脚本语言,其框架受 HTTP 启发。 STOMP 得到广泛支持,心脏长连接一小时左右失去连接 失去到未定义的连接-建立连接后失去连接非常适合在 WebSocket 和 web 上使用。 SockJS SockJS是WebSocket技术的一种模拟,在表面上,它尽可能使用原生webSocket API,但是再底层非常智能,优先使用原生WebSocket,如果在不支持WebSocket的浏览器中,会自动降为轮询的方式。

  • 我使用Iphone 11模拟器。我点击运行按钮并得到这个: 它会打开应用程序并立即关闭。我执行了,但它不起作用。这是我的扑动医生“:

  • 我有一张这样定义的表。其中是主键,是来自另一个表的外键。它在我的笔记本电脑上运行,因此,它是一个本地数据库。在我的表中,我有大约900万条条目。我运行以下命令为表创建新索引: 它运行了大约 10 分钟,我收到以下错误消息: 我检查了我的日志,结果是这样的: 虽然,我收到了一条错误消息,但当我检查索引时,结果是这样的。我不知道问题可能是什么,以及如何解决它,也不知道我创建的索引是否真的有效。有什么想

  • 首先,我需要指出我在我的机器上运行了Wikipedia转储的sql文件,为了能够运行,我需要更新许多关于内存中可用索引大小的设置和一些其他设置。我只想提到那些大型sql查询运行成功,在内存或超时方面没有任何问题。 现在我有了一个表pagelinks(pl_from,pl_title),它显示了出现在每个维基百科页面中的链接,例如,数据可以是(1,“title1”),(1,“title2”),(2,

  • 问题内容: 我正在开发使用JPA + Hibernate访问MySQL数据库的独立服务器。 当我启动服务器时,一切正常。但是,经过一段时间(通常是第二天早上,如果我在下午开始),它将停止工作,因为与MySQL的连接显然已关闭(我在日志中看到很多s)。这可能是由于闲置造成的,服务器正在开发中,晚上没有人使用它。 我以为Hibernate,JDBC或我应用程序下面的其他某些层将管理连接,并在必要时重新