我有一个使用spring-boot,jpa-hiberanate与mysql的应用程序.我收到此错误日志
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56,006,037 milliseconds ago. The last packet sent successfully to the server was 56,006,037 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
Run Code Online (Sandbox Code Playgroud)
这是我的application.properties
# DataSource settings: set here configurations for the database …Run Code Online (Sandbox Code Playgroud) 我在与Maria DB相同的主机上有一个spring-boot应用程序,两者都运行正常一段时间.但是在12小时到2天之间,Spring启动应用程序似乎失去了与数据库的连接(stacktrace),并且无法从中恢复.
当我重新启动弹簧应用程序时,一段时间再次正常.
应用程序没有负载,当它失去连接时,应用程序仍在工作,但数据库连接无法恢复.数据库在此期间没有重启(正常运行时间为4周).只有监控服务对每分钟ping一次DB的应用程序进行ping操作.(春季靴子健康)
连接到同一个数据库的其他Java应用程序运行正常,没有任何问题.
我的问题是:
为什么spring无法从该错误中恢复并尝试重新连接到DB?如何设置弹簧重新连接到DB?
2015-02-19 15:25:48.392 INFO 4931 [qtp92662861-19] --- o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2015-02-19 15:25:48.580 INFO 4931 [qtp92662861-19] --- o.s.jdbc.support.SQLErrorCodesFactory : SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
2015-02-19 15:25:48.616 WARN 4931 [qtp92662861-19] --- o.s.jdbc.support.SQLErrorCodesFactory : Error while extracting database product name - falling back to empty error codes
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. …Run Code Online (Sandbox Code Playgroud) 我有一个 docker-compose 设置来启动我的 SpringBoot 应用程序和一个 MySQL 数据库。如果数据库先启动,那么我的应用程序可以成功连接。但是如果我的应用程序先启动,还没有数据库存在,所以应用程序抛出以下异常并退出:
app_1 | 2018-05-27 14:15:03.415 INFO 1 --- [ main]
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
app_1 | 2018-05-27 14:15:06.770 ERROR 1 --- [ main]
com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization
app_1 | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
Run Code Online (Sandbox Code Playgroud)
我可以编辑我的 docker-compose 文件以确保在应用程序启动之前数据库始终处于启动状态,但我希望应用程序能够自己处理这种情况,而不是在无法到达数据库地址时立即退出。
有多种方法可以在 application.properties 文件中配置数据源,以使应用程序重新连接到数据库,如此处和此处所述。但这不适用于到数据源的启动连接。
如何让我的 SpringBoot 应用程序在启动时以给定的时间间隔重试连接到数据库,直到它成功连接到数据库?
我正在测试一些处理网站注册的代码.java代码如下(摘录):
if (request.getParameter("method").equals("checkEmail")){
String email= request.getParameter("email");
ResultSet rs =null;
PreparedStatement ps = db.prepareStatement(query);
ps.setString(1, email);
rs = ps.executeQuery();
if(rs.next()){
//email already present in Db
} else {
//proceed with registration.....
Run Code Online (Sandbox Code Playgroud)
大多数情况下,该过程执行没有任何问题,但我遇到了一个间歇性问题,因为它与数据库的连接正在关闭.每次失败时,它都会在同一点失败 - 运行上面准备好的语句(检查提交的电子邮件是否已经显然在数据库中).
Postgres的版本是8.1.23
任何帮助或建议表示赞赏.Stacktrace如下(编辑:有时Stacktrace说由Stream Closed导致,有时Socket Closed如下):
13:53:00,973 ERROR Registration:334 - org.postgresql.util.PSQLException: An I/O error occured while sending to the backend.
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:283)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271)
at Registration.doPost(Registration.java:113)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
at …Run Code Online (Sandbox Code Playgroud) 我正在运行Spring Boot连接到PostgreSQL数据库.我已经验证,如果在数据库之后启动Spring Boot,数据将写入数据库.
spring.datasource.url = jdbc:postgresql://localhost/dbname
spring.datasource.username = user
spring.datasource.password = secret
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
Run Code Online (Sandbox Code Playgroud)
当开发中的数据库发生变化时,我会遇到异常:
PreparedStatementCallback; SQL []; This connection has been closed.; nested exception is org.postgresql.util.PSQLException: This connection has been closed.
请注意,异常发生时可以再次访问数据库; 我认为问题是连接是陈旧的,因为它最初连接的数据库端点由于重新启动而不再可用.重新启动Spring Boot可以解决此问题.
如何配置Spring Boot以重新连接到PostgreSQL,以便我不需要重新启动它?
我试图按照Spring Boot JPA中的答案- 配置自动重新连接和如何在spring jpa中关闭连接时如何重新连接数据库?.我不确定PostgreSQL的行为是否与MySQL不同.我阅读Spring Boot文档没有帮助; 我不知道所描述的组件是否足以理解我应该查看哪些文档.
我的春季启动应用程序总是在早上向我显示此whitelabel错误:无法打开JPA EntityManager进行事务处理; 嵌套异常是javax.persistence.PersistenceException:org.hibernate.TransactionException:JDBC开始事务失败:
我搜索网络我认为可能是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') …Run Code Online (Sandbox Code Playgroud) 我使用tomcat连接池使用gradle运行Spring-Boot。所有标准的spring-boot-tools。我在该Web服务器上运行了几个soap-webservices。测试服务器负载时,一切正常。但是在约7.5小时不执行任何操作后,发生此异常。当然可以,timeout但是我尝试通过以下方法防止它:
spring.datasource.url=jdbc:postgresql://mydb?autoReconnect=true@transactional某些陈述。但通常我只使用JPA-Repositoryspring-boot中的from。我的数据库服务器正在运行,PostgreSQL 9.4.1 on x86_64-unknown-linux-gnu并且数据库与应用程序服务器之间没有防火墙。
我需要tcp_keep还活着吗?
为什么我的连接在一定时间后会断开并且无法恢复?
我的应用程序属性:
#
# [ Database Configuration Section ]
#
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
hibernate.format_sql=true
hibernate.hbm2ddl.auto=validate
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
spring.datasource.platform=postgres
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://*****:5434/******
spring.datasource.username=*****
spring.datasource.password=*****
logging.file=*******.log
logging.level.=WARNING
Run Code Online (Sandbox Code Playgroud)
2015-09-29 11:58:50.598 INFO 10498 --- [nio-9092-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2015-09-29 11:58:50.598 INFO 10498 --- [nio-9092-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2015-09-29 11:58:50.674 INFO 10498 --- [nio-9092-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 76 …Run Code Online (Sandbox Code Playgroud) 我们已经开发了一个 Springboot Java 应用程序,我把它放在一个用于开发环境的 docker 容器上。
Spring 启动版本为 1.5.6
使用的 DB 是 SQL server 2016,它再次位于 docker(windows 容器)上。
问题: 每当我重新启动 SQL DB 容器时,应用程序就会开始出现此错误。
2018-10-29 16:00:08,993 ERROR pool-13-thread-1 org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at …Run Code Online (Sandbox Code Playgroud) 我在我的web应用程序中使用spring-boot,spring-jpa,mysql.当我的应用程序运行几个小时后,我总是遇到以下异常:
2016-07-30 21:27:12.434 ERROR 13553 --- [http-nio-8090-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : No operations allowed after connection closed.
2016-07-30 21:27:12.434 WARN 13553 --- [http-nio-8090-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 08003
2016-07-30 21:27:12.434 ERROR 13553 --- [http-nio-8090-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : No operations allowed after connection closed.
2016-07-30 21:27:12.438 ERROR 13553 --- [http-nio-8090-exec-8] [.[.[.[.c.c.Go2NurseJerseyConfiguration] : Servlet.service() for servlet [com.cooltoo.config.Go2NurseJerseyConfiguration] in context with path [] threw exception [org.springframework.dao.DataAccessResourceFailureException: could not prepare statement; nested exception is org.hibernate.exception.JDBCConnectionException: could not prepare statement] with root cause
java.io.EOFException: Can not …Run Code Online (Sandbox Code Playgroud) 根据这个文件:
29.1.1嵌入式数据库支持
Spring Boot可以自动配置嵌入式H2,HSQL和Derby数据库.您不需要提供任何连接URL,只需包含对要使用的嵌入式数据库的构建依赖关系.
和
29.1.2连接到生产数据库
还可以使用池化DataSource自动配置生产数据库连接.
DataSource配置由spring.datasource.*中的外部配置属性控制.例如,您可以在application.properties中声明以下部分:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)
[Tip]您通常不需要指定driver-class-name,因为Spring引导可以从url中为大多数数据库推断出它.
[注意]对于要创建的池化DataSource,我们需要能够验证有效的Driver类是否可用,因此我们在执行任何操作之前检查它.即如果你设置 spring.datasource.driver-class-name = com.mysql.jdbc.Driver,那么该类必须是可加载的.
如果我将以下内容放在application.properties文件中,该怎么办:
spring.datasource.url=jdbc:hsqldb:file:db/organization-db
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
Run Code Online (Sandbox Code Playgroud)
Spring Boot会自动配置池数据源吗,因为我指定了spring.datasource.driver-class-name?
或者它只是为没有连接池的嵌入式数据库驱动程序创建数据源?
如何确认Spring Boot是否正在使用连接池?
spring-boot ×9
java ×6
spring ×5
mysql ×4
postgresql ×3
spring-jdbc ×3
hibernate ×1
hikaricp ×1
io ×1
jdbc ×1
jpa ×1
mariadb ×1
spring-mvc ×1
tomcat ×1