tomcat dbcp 基於jndi配置時出現java.sql.SQLException: Already closed


最近觀察生產環境發現一個現象,一段時間不操作,再重新操作時,數據庫連接第一次會出現:java.sql.SQLException: Already closed.,如下:




error log:  ( org.springframework.dao.RecoverableDataAccessException: PreparedStatementCallback; SQL [select user_id,login_name from t_sys_user where login_name=? and password=?]; The last packet successfully received from the server was1290665 seconds ago.The last packet sent successfully to the server was 1290665 seconds ago, which 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.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was1290665 seconds ago.The last packet sent successfully to the server was 1290665 seconds ago, which 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. )


數據源是用tomcat dbcp 基於jndi配置管理的,查了下,如下:


原因:

You're probably running into the fact that MYSQL closes connections  which have been open "too long". 
Probably if you make the request to the server again, you'll find that  it works because the connection is reopened. 


解決方法:

One way to deal with this is to add the following to your context.xml  Resource: 

validationQuery="select 1"  (基於mysql)

validationQuery="select 1 from dual"  (基於oracle)


代價:

This causes a very cheap test query to always be run first; if the  connection has been closed, this gets the failure, and 
then a new connection is opened.


細想下原來用weblogic jdbc jndi的配置有一個專門的配置項來配置這個test query,配置tomcat時粗心大意了 :(  


[2015-04-30 11:15:46,939 ERROR [com.tencent.vsuning.aop.LogAdvice] - org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"]  數據庫連接池耗盡異常...


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章