让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的用法请你自己到网上搜索吧!这里不再重述。

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