調查原因,原來是DB鏈接失效,mysql 的數據庫有一個全局變量,設定數據庫鏈接有效期限的,默認爲8小時。
如果超過這個時間,一直沒有訪問數據庫的話,mysql會自動將其失效。
解決這個問題有兩個方法:
1.如下,自動激活DB鏈接。
設置參考:DB的context.xml文件,添加下面5行屬性。參數設置:參考官網 http://commons.apache.org/dbcp/configuration.html
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" antiResourceLocking="false">
<Resource name="jdbc/dbname"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://IP.ADD.RE.SS:3306/dbname?characterEncoding=UTF-8"
username="******"
password="******"
maxActive="500"
maxIdle="30"
maxWait="10000"
validationQuery = "SELECT 1"
testWhileIdle = "true"
timeBetweenEvictionRunsMillis = "3600000"
minEvictableIdleTimeMillis = "18000000"
testOnBorrow = "true"/>
</Context>
2.可以修改mysql DB的全局參數。
將默認原來8小時的時效期限更新爲合適的時長。
The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect().
interactive_timeout:
The number of seconds the server waits for activity on a noninteractive connection before closing it. This timeout applies only to TCP/IP and Unix socket file connections, not to connections made using named pipes, or shared memory.
問題:
如果在配置文件my.cnf中只設置參數wait_timeout=100,則重啓服務器後進入,
執行: mysql> show variables like “%timeout%”
要同時設置interactive_timeout和wait_timeout纔會生效。
set wait_timeout=100
set interactive_timeout=100
重啓MySQL Server進入後,查看設置已經生效。