MYSQL DBCP JNDI TOMCAT6 隔天或超過8小時訪問 DB鏈接失效 報錯 如何來配置context.xml文件

現象:網站上線後,由於開始訪問量不是很多,休息天一般都無人訪問。發現星期一上班過來,打開網站,報頁面找不到的應用錯誤。
調查原因,原來是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小時的時效期限更新爲合適的時長。

wait_timeout:
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%”

會發現參數設置並未生效,仍然爲28800(即默認的8個小時)。

要同時設置interactive_timeout和wait_timeout纔會生效。
set wait_timeout=100
set interactive_timeout=100

重啓MySQL Server進入後,查看設置已經生效。


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