サーバの移行作業をしていて気が付いたのですが、DBCPを使ってTomcatからmysqlへアクセスする場合、一定期間(8時間?)以上の間隔が空くと、次のような例外が発生するようです。
しかし、MySQLのサイトに「autoReconnect=trueを使わないように」との記述があるのを見つけました。こんなふうに書いてあります。
http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html
ようするに、自動再接続を行うのではなく、コネクションのテストをしなさいということのようです。コンテキストファイルを次のようにしてみました(Tomcat5.5以降の設定方法です)。
今、設定を変えてみたばかりなので、これでうまくいくかどうかはまだ分かりません。8時間後に報告します。
java.sql.SQLException: Io exception: Broken pipe
MySQLはリソースの浪費を防ぐために、一定時間以上使われなかったコネクションは自動的に閉じるようになっています。私が書いた本では、コネクションが切れた場合に、自動的に再接続するように「autoReconnect=true」を指定するように説明しました。Tomcatのドキュメントにも同様の説明があります。しかし、MySQLのサイトに「autoReconnect=trueを使わないように」との記述があるのを見つけました。こんなふうに書いてあります。
<!-- Don't use autoReconnect=true, it's going away eventually
and it's a crutch for older connection pools that couldn't
test connections. You need to decide whether your application
is supposed to deal with SQLExceptions (hint, it should), and
how much of a performance penalty you're willing to pay
to ensure 'freshness' of the connection -->
情報元:and it's a crutch for older connection pools that couldn't
test connections. You need to decide whether your application
is supposed to deal with SQLExceptions (hint, it should), and
how much of a performance penalty you're willing to pay
to ensure 'freshness' of the connection -->
http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html
ようするに、自動再接続を行うのではなく、コネクションのテストをしなさいということのようです。コンテキストファイルを次のようにしてみました(Tomcat5.5以降の設定方法です)。
<?xml version='1.0' encoding='utf-8'?>
<Context docBase = "/Users/takegata/prg/jamyto_forum/jsp" path="/takegata">
<Resource name="jdbc/jamyto_forum"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
auth="Container"
type="javax.sql.DataSource"
maxActive="10"
maxIdle="30"
maxWait="1000"
username="Mulder"
password="TrustNo1"
driverClassName="com.mysql.jdbc.Driver"
testOnBorrow="true"
validationQuery="SELECT 1"
url="jdbc:mysql://localhost/jamyto_forum?useUnicode=true&characterEncoding=utf8"
/>
</Context>
testOnBorrow="true"は、プーリングからコネクションを取得する際にテストを行うための指定です。テスト用のクエリを validationQuery="SELECT 1" で指定しています。<Context docBase = "/Users/takegata/prg/jamyto_forum/jsp" path="/takegata">
<Resource name="jdbc/jamyto_forum"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
auth="Container"
type="javax.sql.DataSource"
maxActive="10"
maxIdle="30"
maxWait="1000"
username="Mulder"
password="TrustNo1"
driverClassName="com.mysql.jdbc.Driver"
testOnBorrow="true"
validationQuery="SELECT 1"
url="jdbc:mysql://localhost/jamyto_forum?useUnicode=true&characterEncoding=utf8"
/>
</Context>
今、設定を変えてみたばかりなので、これでうまくいくかどうかはまだ分かりません。8時間後に報告します。
投稿:竹形 誠司[takegata]/2008年 03月 11日 05時 09分
/更新:2008年 03月 11日 15時 22分
その後
by 竹形 誠司[takegata]
8時間以上放置して、その後アクセスしてみましたが、Broken pipeの例外は出ませんでした。もう少し様子を見てみます。
投稿:竹形 誠司[takegata]/2008年 03月 11日 15時 11分
/更新:2008年 03月 11日 15時 11分