讓Hibernate自動重新連接數據庫——使用c3p0連接池

Hibernate沒有自動重新連接數據庫,原因很可能是因爲你使用了Hibernate內置的連接池,這個連接池不會自動重新連接。使用Mysql時,默認過8小時沒有數據交換,Mysql就會單方面斷開數據庫連接,所以有些時候你會發現過了8小時(或者一晚上)再訪問網站,程序就會拋出數據庫鏈接錯誤,而重啓服務器容器(Tomcat等)之後程序又恢復正常。

        細心的你應該會發現,如果使用默認的連接池,在Hibernate的日誌記錄(INFO級別)中會提示:Using Hibernate built-in connection pool (not for production use!)(“你現在使用的是Hibernate內置的連接池,請不要在產品中使用它!”)如果你不聽勸告,仍然使用它,就會出現上面所說的數據庫單方面斷開連接而無法訪問的問題。

        解決此方法就是使用其他的連接池,比如此c3p0,在Hibernate官網的參考手冊(reference)的3.3節中有簡單的介紹①:

/**********引用開始*********/

Hibernate's own connection pooling algorithm is however quite rudimentary. It is intended to help you get started and is not intended for use in a production system or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use C3P0.

C3P0 is an open source JDBC connection pool distributed along with Hibernate in the lib directory. Hibernate will use its org.hibernate.connection.C3P0ConnectionProvider for connection pooling if you set hibernate.c3p0.* properties. If you'd like to use Proxool refer to the packaged hibernate.properties and the Hibernate web site for more information.

Here is an example hibernate.properties file for C3P0:

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
/**********引用結束*********/
到這裏還沒結束,如果你按上面的方法添加了,不一定可以成功使用c3p0連接池(至少我測試時沒有成功),你還需要添加一句話:

hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider

官方參考文檔指出connection.provider_class屬性是在自定義連接提供者時使用的,並沒有說使用c3p0時也要加這句話。但我測試時不加這句話就沒有效果,所以還是加上吧!

完整的設置方法:

(屬性文件)

/*************排版開始**************/

# Database connection settings
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/mydb
hibernate.connection.username=username
hibernate.connection.password=password
# configuration pool
hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=15
hibernate.c3p0.timeout=3600
hibernate.c3p0.max_statements=50
# SQL dialect
hibernate.dialect=org.hibernate.dialect.MySQLDialect
# Enable Hibernate's automatic session context management
hibernate.current_session_context_class=thread
# Echo all executed SQL to stdout
hibernate.show_sql=true
# Drop and re-create the database schema on startup
hibernate.hbm2ddl.auto=update

/*************排版結束**************/

如果你想把properties放到xml中,使用hibernate.c3p0.min_size等4項,(而不是c3p0.min_size)不然可能會扔出警告。

/*********排版開始***********/

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">15</property>
<property name="hibernate.c3p0.timeout">3600</property> <!-- seconds -->
<property name="hibernate.c3p0.max_statements">50</property>

/**********排版結束****************/

關於更多c3p0的用法請你自己到網上搜索吧!這裏不再重述。

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