1.尝试登录网站时失败,查看发现出现以下错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 259,809,499
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.
2.根据报错信息可以判断连接池中的连接Connection失效,即超过了有效时间wait_timeout。登录mysql后查看wait_timeout有效时间为28800,即八小时。
show global variables like 'wait_timeout';
3.修改wait_timeout属性,调大数值为31536000(365天,linux最大值为一年)。在my.cnf配置文件中,[mysqlId]下添加wati_timeout、interactive_timeout属性,重启mysql。此时测试发现已经可以连接,基本解决了问题。
(1).编辑my.cnf配置文件
vi /etc/my.cnf
(2).添加wait_timeout属性
[mysqlId]
...
wait_timeout = 31536000
interactive_timeout = 31536000
(3).重启mysql
service mysqld restart
4.为了保证连接池的连接有效,所以修改连接池属性如下:
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dbcp.driver}" />
<property name="url" value="${dbcp.url}" />
<property name="username" value="${dbcp.username}" />
<property name="password" value="${dbcp.password}" />
<property name="maxActive" value="${dbcp.maxActive}"/>
<property name="minIdle" value="${dbcp.minIdle}"/>
<property name="maxWait" value="${dbcp.maxWait}"/>
<property name="timeBetweenEvictionRunsMillis" value="64800" />
<property name="testWhileIdle" value="true" />
<property name="validationQuery" value="select 1 from dual" />
<property name="testOnBorrow" value="true" />
</bean>
参考自:http://zeusami.iteye.com/blog/1112827