Hibernate的数据库连接池

Hibernate的数据库连接池是非常简陋的。并不适合我们在开发产品时候是用。当我们是用配置:

<property name="connection.pool_size">1</property>
这个配置的时候,默认会使用Hibernate自己的数据库连接池,这种配置非常不推荐。

可以使用Hibernate集成的c3p0。C3P0是一个开源的JDBC连接池。它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。

在配置文件中使用下面的配置来替换:

<!-- JDBC connection pool (use the built-in) -->
        <!--
        <property name="connection.pool_size">1</property>
                设置数据库连接池的实现方式为c3p0
		  -->
		
		<!-- 最小连接数 -->
		<property name="hibernate.c3p0.min_size">5</property>
		<!-- 最大连接数 -->
		<property name="hibernate.c3p0.max_size">20</property>
		<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
		<property name="hibernate.c3p0.timeout">1800</property>
		<!-- 最大的PreparedStatement的数量 -->
		<property name="hibernate.c3p0.max_statements">50</property>
		<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
		<property name="hibernate.c3p0.idle_test_period">120</property>
		<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
		<property name="hibernate.c3p0.acquire_increment">2</property>
		<!-- 每次都验证连接是否可用 -->
		<property name="hibernate.c3p0.validate">true</property>

这样就可以使用C3P0对Hibernate的数据库连接进行管理。

在Hibernate的语句执行之前还可以自己定义需要执行的文件对数据库进行初始化:

 <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
        <property name="hibernate.hbm2ddl.import_files">persons.sql</property>
        <!-- 
        File order matters, the statements of a give file are executed 
        before the statements of the following files.
        These statements are only executed if the schema is created ie 
        if hibernate.hbm2ddl.auto is set to create or create-drop.
		e.g. /humans.sql,/dogs.sql 
         -->
需要注意的是这里的:

<property name="hbm2ddl.auto">create</property>
必须为create或者drop:

persons.sql:

delete from wife where 1=1
insert into wife(name) values("lisi")
insert into wife(name) values("wanger")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值