1. 程式人生 > >mysql No operations allowed after connection closed連線異常

mysql No operations allowed after connection closed連線異常

問題描述:專案中使用了mysql+c3p0,部署完後當天沒有出異常,第二天訪問時報異常

                異常描述 No operations allowed after connection closed

原因:MySQL5.0以後針對超長時間DB連線做了一個處理,Mysql伺服器預設的“wait_timeout”是8小時,也就是說一個connection空閒超過8個小時,Mysql將自動斷開該 connection。這就是問題的所在,在C3P0 pools中的connections如果空閒超過8小時,Mysql將其斷開,而C3P0並不知道該connection已經失效,如果這時有 Client請求connection,C3P0將該失效的Connection提供給Client,將會造成上面的異常。

解決的方法有3種:

         增加wait_timeout的時間。

         減少Connection pools中connection的lifetime。

         測試Connection pools中connection的有效性。

C3P0增加以下配置資訊:

//獲取connnection時測試是否有效

testConnectionOnCheckin = true

//自動測試的table名稱

automaticTestTable=C3P0TestTable

//set to something much less than wait_timeout, prevents connections from going stale

idleConnectionTestPeriod = 18000

//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out

maxIdleTime = 25000

//if you can take the performance 'hit', set to "true"

testConnectionOnCheckout = true

原文:http://www.blogjava.net/Alpha/archive/2009/03/29/262789.html