让Hibernate自动重新连接数据库——使用c3p0连接池
2012-11-09 09:46
501 查看
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
libdirectory. Hibernate will use its
org.hibernate.connection.C3P0ConnectionProviderfor connection pooling if you set hibernate.c3p0.* properties. If you'd like to use Proxool refer to the packaged
hibernate.propertiesand the Hibernate web site for more information.
Here is an example
hibernate.propertiesfile 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的用法请你自己到网上搜索吧!这里不再重述。
注释:
①引用的出处(2008年9月10日 18:26:56修改版):http://docs.jboss.org/hibernate/stable/core/reference/en/html/configuration-hibernatejdbc.html
相关文章推荐
- 让Hibernate自动重新连接数据库——使用c3p0连接池
- myeclipse 建立mysql数据库连接及使用MyEclipse自动生成hibernate的数据库表
- hibernate自动重新连接数据库
- hibernate 使用连接池连接数据库时 自动断开处理
- hibernate使用jtds数据库驱动类利用windows认证方式连接remote sql server
- Hibernate使用configuration调用buildFactory()或得session连接数据库
- 在MyEclipse的web项目/java项目中,使用Hibernate-tools中的hbm2java和hbm2ddl工具,根据hbm文件自动生成pojo和数据库脚本
- 使用hibernate连接mysql自动中断的问题
- spring与hibernate整合-使用properties文件分离数据库连接设置
- web程序通过dbcp连接池处理自动重新连接数据库问题(通过连接池dbcp处理方案)
- Hibernate中使用了序列给主键自动增长,但是添加记录到数据库时报违反唯一性约束??
- 02-使用C3P0连接池连接MySql并且使用QueryRunner简化数据库操作
- 使用 pywin32 中 adodbapi 连接数据库, 关闭连接后重新打开出现异常
- linux使用脚本自动连接数据库
- 使用Hibernate连接h2数据库的配置文件写的方法(连接字符串)
- putty完全使用手册--多窗口---git提交---连接数据库--自动日志显示
- 使用hibernate根据配置的实体bean类无法自动创建数据库表
- SSH中hibernate过了8个小时后自动关闭与数据库的连接
- 使用Hibernate生成数据库和连接数据库
- 使用Hibernate连接多个数据库