com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
新建的網站出現如下錯誤:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 58,524,080 milliseconds ago. The last packet sent successfully to the server was 58,524,127 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.
?
Caused by: java.net.SocketException: Software caused connection abort: socket write error
?
網上找來的答案:.
在mysql安裝目次下找到my.ini文件中添加超時限制:在該文件最后添加一行:wait_timeout=2880000。如許把之前的超時限制把8小時(28800)擴大為800小時。如許重啟了mysql后再在此中輸入號令:show global variables like "wait_timeout";查看超時是否已批改為:2880000。重啟tomcat(若是已啟動),OK。
?
?
1
解決辦法:從common pool的配置參數來解決:
?<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
? <property name="driverClassName">
? ?<value>${db.driver}</value>
? </property>
? <property name="url">
? ?<value>${db.url}</value>
? </property>
? <property name="username">
? ?<value>${db.user}</value>
? </property>
? <property name="password">
? ?<value>${db.password}</value>
? </property>
? <property name="maxActive">
? ?<value>100</value>
? </property>
? <property name="maxIdle">
? ?<value>50</value>
? </property>
? <property name="maxWait">
? ?<value>10000</value>
? </property>
? <property name="timeBetweenEvictionRunsMillis">
? ?<value>3600000</value><!--1 hours-->
? </property>
<!--
? <property name="minEvictableIdleTimeMillis">
? ?<value>20000</value>
? </property>
-->
??
? <property name="testWhileIdle">
? ?<value>true</value>
? </property>
? <property name="validationQuery">
? ?<value>select 1 from dual</value>
? </property>
?</bean>
使用上述的三個紅色的參數,就可以避免這個問題.這三個參數的意義:
timeBetweenEvictionRunsMillis:啟動connection校驗定時器,定時器運行時間間隔就是timeBetweenEvictionRunsMillis的值.默認為-1,表示不啟動定時器,這里設定為1小時,只要小于mysql的wait_timeout就可以了
testWhileIdle: true,表示檢查idle的connection,false為不檢查
validationQuery:用于檢查connection的sql語句.
這只是一種方法,另外的幾種方法:
timeBetweenEvictionRunsMillis+minEvictableIdleTimeMillis:這種方式不檢查Connection的有效性,而是檢查連接的空閑時間,大于minEvictableIdleTimeMillis就清除.
? <property name="timeBetweenEvictionRunsMillis">
? ?<value>3600000</value><!--1 hours-->
? </property>
? <property name="minEvictableIdleTimeMillis">
? ?<value>120000</value><!--connection的空閑時間大于這個值,就直接被關閉,并從連接池中刪除-->
? </property>
如果不喜歡用定時器,也可以配置testOnBorrow+validationQuery參數:每次從連接池取參數都會校驗連接的有效性.實際上這種方式性能會比定時器差些.
? <property name="testOnBorrow">
? ?<value>true</value>
? </property>
? <property name="validationQuery">
? ?<value>select 1 from dual</value>
? </property>
另外,也可以用testOnReturn+validationQuery,不過未必會解決問題:這表示每次使用完連接,歸還連接池的時候檢查連接的有效性,這有可能導致使用一次無效的連接,最好不要用.
上面的幾種方法可以合并使用,只是檢查的點多了,未必是好事.
轉載于:https://www.cnblogs.com/stonehat/archive/2013/01/17/2864247.html
總結
以上是生活随笔為你收集整理的com.mysql.jdbc.exceptions.jdbc4.CommunicationsException的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果系统从零开始--MAC OS X 教
- 下一篇: 工期OR质量