2016-09-26 72 views
2

我有一個與MySQL DB交互的Java web應用程序。我從不關閉數據庫連接,並使用單例來處理連接實例。MySQL連接不見了Java

的問題是,我保持連接後打開太久,我嘗試做在應用中的一些動作,我收到此錯誤:

The last packet successfully received from the server was 211,710,369 milliseconds ago. The last packet sent successfully to the server was 211,710,398 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. 

任何想法,我怎麼能做出autoReconnect=true?或者爲什麼會發生?我雖然會默認自動重新連接。還是應該在每次請求後關閉數據庫?這是降低性能這是至關重要的這個應用程序。

我可以提供更多信息嗎?

+2

您是否真的讓連接閒置了2.45天? –

+1

如果連接閒置2½天,則打開和關閉連接不會是性能問題。 –

+1

經典。性能問題。 – Drew

回答

2

做到這一點。雖然你不應該讓開放,連接長..但要回答有關問題autoReconnect的。它的屬性可以設置在連接網址中,例如:

url="jdbc:mysql://localhost:3306/confluence?autoReconnect=true" 
0

使用以下,以保持連接

首先使用這種用於MySQL連接

jdbc:mysql://localhost/<database>?autoReconnect=true 

使用驗證查詢,以保持連接活活動的連接也很長一段時間。

http://www.codingpedia.org/ama/tomcat-jdbc-connection-pool-configuration-for-production-and-development/

在springboot應用程序,您可以通過添加以下配置中的

datasource.primary.testWhileIdle=true  
datasource.primary.timeBetweenEvictionRunsMillis=10000  
datasource.primary.validationQuery=SELECT 1  
datasource.primary.testOnBorrow=true