配置Hibernate连接池

本文介绍Hibernate默认连接池的配置及如何使用C3P0和Proxool连接池替代默认连接池,提高数据库连接管理和性能。
Hibernate自带的连接池性能不高,而且还存在BUG,因此官方推荐使用c3p0Proxool连接池。
<o:p></o:p>

    1Hibernate默认连接池<o:p></o:p>

<?xml version='1.0' encoding='UTF-8'?><o:p></o:p>

<!DOCTYPE hibernate-configuration<o:p></o:p>

PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"<o:p></o:p>

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><o:p></o:p>

<hibernate-configuration><o:p></o:p>

 <o:p></o:p>

<session-factory ><o:p></o:p>

 <o:p></o:p>

<!—JDBC驱动程序--><o:p></o:p>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property><o:p></o:p>

<!-- 连接数据库的URL--><o:p></o:p>

<property name="connection.url"> <o:p></o:p>

jdbc:mysql://localhost:3306/schoolproject<o:p></o:p>

</property><o:p></o:p>

<property name="connection.useUnicode">true</property><o:p></o:p>

<property name="connection.characterEncoding">UTF-8</property><o:p></o:p>

<!--连接的登录名--><o:p></o:p>

<property name="connection.username">root</property><o:p></o:p>

<!—登录密码--><o:p></o:p>

<property name="connection.password"></property><o:p></o:p>

 <o:p></o:p>

<!--是否将运行期生成的SQL输出到日志以供调试--><o:p></o:p>

<property name="show_sql">true</property><o:p></o:p>

<!--指定连接的语言--><o:p></o:p>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property><o:p></o:p>

<!--映射Student这个资源--><o:p></o:p>

<mapping resource="com/wqbi/model/pojo/student.hbm.xml" /><o:p></o:p>

</session-factory> <o:p></o:p>

</hibernate-configuration><o:p></o:p>

 <o:p></o:p>

    2C3P0连接配置<o:p></o:p>

<?xml version='1.0' encoding='UTF-8'?><o:p></o:p>

<!DOCTYPE hibernate-configuration<o:p></o:p>

PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"<o:p></o:p>

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><o:p></o:p>

<hibernate-configuration><o:p></o:p>

 <o:p></o:p>

<session-factory ><o:p></o:p>

<!—JDBC驱动程序--><o:p></o:p>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property><o:p></o:p>

<!-- 连接数据库的URL--><o:p></o:p>

<property name="connection.url"> <o:p></o:p>

jdbc:mysql://localhost:3306/schoolproject<o:p></o:p>

</property><o:p></o:p>

<property name="connection.useUnicode">true</property><o:p></o:p>

<property name="connection.characterEncoding">UTF-8</property><o:p></o:p>

<!--连接的登录名--><o:p></o:p>

<property name="connection.username">root</property><o:p></o:p>

<!--登录密码--><o:p></o:p>

<property name="connection.password"></property><o:p></o:p>

<!-- C3P0连接池设定--><o:p></o:p>

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider<o:p></o:p>

</property><o:p></o:p>

<property name="hibernate.c3p0.max_size">20</property><o:p></o:p>

<property name="hibernate.c3p0.min_size">5</property><o:p></o:p>

<property name="hibernate.c3p0.timeout">120</property><o:p></o:p>

<property name="hibernate.c3p0.max_statements">100</property><o:p></o:p>

<property name="hibernate.c3p0.idle_test_period">120</property><o:p></o:p>

<property name="hibernate.c3p0.acquire_increment">2</property><o:p></o:p>

<!--是否将运行期生成的SQL输出到日志以供调试--><o:p></o:p>

<property name="show_sql">true</property><o:p></o:p>

<!--指定连接的语言--><o:p></o:p>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property><o:p></o:p>

<!--映射Student这个资源--><o:p></o:p>

<mapping resource="com/wqbi/model/pojo/student.hbm.xml" /><o:p></o:p>

</session-factory> <o:p></o:p>

</hibernate-configuration><o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

    3proxool连接池<o:p></o:p>

(1)     先写proxool的配置文件,文件名:proxool.xml(一般放在与hibernate.cfg.xml文件在同一个目录中)本例配置的是MYSQL数据库,数据库的名字为schoolproject<o:p></o:p>

 <o:p></o:p>

<?xml version="1.0" encoding="UTF-8"?> <o:p></o:p>

<!-- the proxool configuration can be embedded within your own application's. <o:p></o:p>

Anything outside the "proxool" tag is ignored. --> <o:p></o:p>

 <o:p></o:p>

<something-else-entirely><o:p></o:p>

<proxool><o:p></o:p>

<!--连接池的别名--><o:p></o:p>

<alias>DBPool</alias><o:p></o:p>

<!--proxool只能管理由自己产生的连接--><o:p></o:p>

<driver-url><o:p></o:p>

jdbc:mysql://localhost:3306/schoolproject?useUnicode=true&amp;characterEncoding=UTF8<o:p></o:p>

</driver-url><o:p></o:p>

<!—JDBC驱动程序--><o:p></o:p>

<driver-class>com.mysql.jdbc.Driver</driver-class><o:p></o:p>

<driver-properties><o:p></o:p>

<property name="user" value="root"/><o:p></o:p>

<property name="password" value=""/><o:p></o:p>

</driver-properties> <o:p></o:p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值