2013年8月14日 星期三

hibernate connection 隔夜被斷線的問題

最近因為一個Hibernate + Spring + Quartz  的Web專案,  執行完Quartz Scheduler  的Job後,  隔天早上就出現

SQL Error: 17002, SQLState: null
IO 異常: Connection reset
java.sql.SQLException: 關閉的連線

然後就是一堆
SQL Error: 0, SQLState: null
Already closed.
重覆的錯誤

(hibernate connection close overnight的問題, 隔夜connection中斷問題,或是自動重新連線問題…)

Hibernate官網論壇說到

You should never use Hibernate's built-in connection pooling in production; it is non-scalable and non-fault-tolerant. Instead, use DBCP or C3P0 (or, even better, an application server datasource).

Automatic reconnect from Hibernate to MySQL 提出用C3P0

CrazyTechThoughts 在 Resolve Hibernate connection timeout issue with MySQL server 也提出用 c3p0.testConnectionOnCheckout=true 的方法

StackOverflow也有人問 Hibernate, C3P0, Mysql — Broken Pipe

 

好像只能換Connection pool 的技術了, 但我不想將DBCP改為C3P0, 因為還有大部份舊的hibernate mapping 還在運行, 換了之後不知道還要花多少時間測試.

最後我的解決方式:

在 spring 的設定檔內

<bean>

<property name="validationQuery" value="SELECT 1 FROM DUAL" />
<property name="testOnBorrow" value="true" />

</bean>

因為我用的是OracleDB, 所以用 Select 1 From Dual, 其它資料庫請自行修改.

快一週了,好像還可以, 沒聽到有問題.

沒有留言: