在spring-boot jpa hibernate中,在> 4 <24之后連接到Db我有一個使用spring-boot,jpa-hiberanate與mysql的應(yīng)用程序。我收到此錯誤日志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.這是我的application.properties# DataSource settings: set here configurations for the database connectionspring.datasource.url = jdbc:mysql://localhost:3306/testspring.datasource.username = test
spring.datasource.password = test
spring.datasource.driverClassName = com.mysql.jdbc.Driver# Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sql queryspring.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.MySQL5Dialectspring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy要解決這個問題,我可以使用spring.datasource.testOnBorrow=truespring.datasource.validationQuery=SELECT 1但我檢查了它不推薦。所以任何人都可以建議我應(yīng)該怎么做才能克服這個錯誤
1 回答

莫回?zé)o
TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊
最簡單的方法是autoReconnect
在JDBC URL中指定屬性,盡管這不是推薦的方法。
spring.datasource.url = jdbc:mysql://localhost:3306/test?autoReconnect=true
當(dāng)您有活動連接時,這會產(chǎn)生問題,并且在事務(wù)發(fā)生時會發(fā)生某些事情,并且會發(fā)生重新連接。在事務(wù)開始時驗證連接并在開始時獲取新連接時,它不會產(chǎn)生問題。
但是,最好在應(yīng)用程序的生命周期內(nèi)啟用連接驗證。為此,您可以指定多個屬性。
首先,指定允許池的最大連接數(shù)。(有關(guān)確定max poolsize的讀取,請閱讀此內(nèi)容)。
spring.datasource.max-active=10
您還可能需要指定初始連接數(shù)
spring.datasource.initial-size=5
接下來,您要指定空閑連接的最小和最大數(shù)量。
spring.datasource.max-idle=5spring.datasource.min-idle=1
要驗證連接,您需要指定驗證查詢以及何時驗證。您想要定期驗證,而不是從池中檢索連接(這是為了防止池中的連接斷開)。
spring.datasource.test-while-idle=truespring.datasource.test-on-borrow=truespring.datasource.validation-query=SELECT 1
現(xiàn)在您還在連接空閑時進(jìn)行驗證,您需要指定為連接運行此查詢的頻率以及何時將連接視為空閑。
spring.datasource.time-between-eviction-runs-millis=5000 (this is the default)spring.datasource.min-evictable-idle-time-millis=60000 (this is also default)
這一切都應(yīng)該觸發(fā)您的(空閑)連接的驗證,并且當(dāng)發(fā)生異?;蚩臻e時段已經(jīng)過去時,您的連接將從池中刪除。
假設(shè)您使用Tomcat JDBC作為連接池,這是一個很好的讀取配置的內(nèi)容和方法。
添加回答
舉報
0/150
提交
取消