hibernate配置c3p0 連接池

 

項目中使用tomcat5.0+mysql4.1,struts+hibernate,其JDBC驅動爲3.1.12版本,第一天運行調試一切正常。可是到了第二天,只要一登錄就提示"No operations allowed after connection closed"異常。在這種情況下只要重新啓動Tomcat就恢復正常,然而到了第二天問題依舊。原來Mysql在經過8小時不使用後會自動關閉已打開的連接。

解決方法之一是不使用Hibernate內置的連接池,改用C3P0連接池,這個連接池會自動處理數據庫連接被關閉的情況。要使用C3P0很簡單,先從Hibernate裏把c3p0-0.8.3.jar複製到項目的lib目錄中,再配置Hibernate配置文件。

Hibernate配置文件可以有兩種格式,一種是hibernate.properties,另一種是hibernate.cfg.xml,後者稍微方便一些,當增加hbm映射文件的時候,可以直接在hibernate.cfg.xml裏面增加,不必像hibernate.properties必須在初始化代碼中加入。但不管怎麼說,兩種的配置項都是一樣的。

hibernate.properties中的配置
hibernate.show_sql = true
hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/HibernateTest
hibernate.connection.username = root
hibernate.connection.password =
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50

hibernate.cfg.xml中的配置
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>

<mapping resource="User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

 

 

最近的一個項目在Hibernate使用C3P0的連接池,數據庫爲Mysql。開發測試沒有問題,在運行中每個一段長的空閒時間就出現異常:

java 代碼

  1. org.hibernate.exception.JDBCConnectionException: could not execute query
  2. at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
  3. at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
  4. .......
  5. Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:
  6. ** BEGIN NESTED EXCEPTION **
  7. com.mysql.jdbc.CommunicationsException
  8. MESSAGE: Communications link failure due to underlying exception:
  9. ** BEGIN NESTED EXCEPTION **
  10. java.net.SocketException
  11. MESSAGE: Broken pipe
  12. STACKTRACE:
  13. java.net.SocketException: Broken pipe
  14. at java.net.SocketOutputStream.socketWrite0(Native Method)
  15. ......
  16. ** END NESTED EXCEPTION **

查看了Mysql的文檔,以及Connector/J的文檔以及在線說明發現,出現這種異常的原因是:

Mysql服務器默認的“wait_timeout”是8小時,也就是說一個connection空閒超過8個小時,Mysql將自動斷開該connection。這就是問題的所在,在C3P0 pools中的connections如果空閒超過8小時,Mysql將其斷開,而C3P0並不知道該connection已經失效,如果這時有Client請求connection,C3P0將該失效的Connection提供給Client,將會造成上面的異常。

解決的方法有3種:

  1. 增加wait_timeout的時間。
  2. 減少Connection pools中connection的lifetime。
  3. 測試Connection pools中connection的有效性。

當然最好的辦法是同時綜合使用上述3種方法,下面就DBCP和C3P0分別做一說明,假設wait_timeout爲默認的8小時

DBCP增加以下配置信息:

  1. //set to 'SELECT 1'
  2. validationQuery = "SELECT 1"
  3. //set to 'true'
  4. testWhileIdle = "true"
  5. //some positive integer
  6. timeBetweenEvictionRunsMillis = 3600000
  7. //set to something smaller than 'wait_timeout'
  8. minEvictableIdleTimeMillis = 18000000
  9. //if you don't mind a hit for every getConnection(), set to "true"
  10. testOnBorrow = "true"

C3P0增加以下配置信息:

  1. //獲取connnection時測試是否有效
  2. testConnectionOnCheckin = true
  3. //自動測試的table名稱

  4. automaticTestTable=C3P0TestTable

  5. //set to something much less than wait_timeout, prevents connections from going stale
  6. idleConnectionTestPeriod = 18000
  7. //set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
  8. maxIdleTime = 25000
  9. //if you can take the performance 'hit', set to "true"
  10. testConnectionOnCheckout = true

更多的配置信息大家可以查看C3P0文檔,Connector/J文檔,以及DBCP的文檔。

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