aopicoのブログ -21ページ目

aopicoのブログ

ブログの説明を入力します。
→しません。

以下の例外が発生した。

java.sql.SQLException : Already closed.
The last packet successfully received from the server was 52,069,152 milliseconds ago.
The last packet sent successfully to the server was 52,069,153 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.

どうやら、JNDIを使用していて一定時間DBへの接続がなくなると、維持(プーリング)している接続を自動的に切断される事から起こる例外で、再接続後1回目のクエリ送信はSQLExceptionが発生するものの、2回目以降のクエリ送信は通常通り処理されるようだ。

という訳で、
TomcatのContext.xmlのResource要素内に以下の2行を追加。

testOnBorrow="true"
validationQuery を試し、 接続が正常である事を確認する。
validationQuery="select 1 from dual"
DB との接続テストに用いる SQL SELECT 文

--Context.xmlの記述例--
<
Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
username="[mysqlユーザー名]"
password="[mysqlユーザのパスワード]"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:[mysql接続ポート番号]/[接続テーブル名]"
maxActive="50"
maxIdle="50"
maxWait="-1"
testOnBorrow="true"
validationQuery="select 1 from dual"
removeAbandoned="false"
/>

対策として、検証用のクエリを送信しましょうという事らしい。

解説などは他のサイトで詳しくあるので割愛して解決法のみ記します。