Java 在 spring-boot jpa 休眠中 >4<24 后,与 Db 的连接终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30451470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connection to Db dies after >4<24 in spring-boot jpa hibernate
提问by Soham
I have an app that uses spring-boot,jpa-hiberanate with mysql.I am getting this error log
我有一个使用 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.
Here is my application.properties
这是我的 application.properties
# DataSource settings: set here configurations for the database connection
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = test
spring.datasource.password = test
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
To solve this issue I can use
为了解决这个问题,我可以使用
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
But I checked that it's not recommended.So can anyone suggest me what should I do to overcome this error
但我检查了它是不推荐的。所以任何人都可以建议我应该怎么做才能克服这个错误
采纳答案by M. Deinum
The easiest way is to specify the autoReconnect
property in the JDBC url, although this isn't the recommended approach.
最简单的方法是autoReconnect
在 JDBC url 中指定属性,尽管这不是推荐的方法。
spring.datasource.url = jdbc:mysql://localhost:3306/test?autoReconnect=true
This can give issues when you have an active connection and during a transaction something happens and a reconnect is going to happen. It will not give issues when the connection is validated at the start of the transaction and a new connection is acquired at the start.
当您有活动连接并且在事务期间发生某些事情并且将要发生重新连接时,这可能会产生问题。当在事务开始时验证连接并在开始时获取新连接时,它不会出现问题。
However it is probably better to enable validation of your connections during the lifetime of your application. For this you can specify several properties.
但是,在应用程序的生命周期内启用连接验证可能会更好。为此,您可以指定多个属性。
First start by specifying maximum number of connections you allow for the pool. (For a read on determining the max poolsize read this).
首先指定您允许池的最大连接数。(有关确定最大池大小的阅读,请阅读此内容)。
spring.datasource.max-active=10
You also might want to specify the number of initial connections
您可能还想指定初始连接数
spring.datasource.initial-size=5
Next you want to specify the min and max number of idle connections.
接下来您要指定最小和最大空闲连接数。
spring.datasource.max-idle=5
spring.datasource.min-idle=1
To validate connection you need to specify a validation-query and when to validate. As you want to validate periodically, instead of when a connection is retrieved from the pool (this to prevent broken connections in your pool).
要验证连接,您需要指定验证查询以及验证时间。因为您想定期验证,而不是在从池中检索连接时进行验证(这是为了防止池中的连接断开)。
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1
Now that you are also validating while a connection is idle you need to specify how often you want to run this query for the connections and when a connection is considered idle.
既然您还在连接空闲时进行验证,您需要指定要为连接运行此查询的频率以及连接被视为空闲的时间。
spring.datasource.time-between-eviction-runs-millis=5000 (this is the default)
spring.datasource.min-evictable-idle-time-millis=60000 (this is also default)
This all should trigger validation of your (idle) connections and when an exception occurs or the idle period has passed your connections will be removed from the pool.
这一切都应该触发对您的(空闲)连接的验证,并且当发生异常或空闲期已过时,您的连接将从池中删除。
Assuming you are using Tomcat JDBC as the connection pool thisis a nice read of what and how to configure.
假设您使用 Tomcat JDBC 作为连接池,这是一个很好的阅读配置内容和方式的文章。