hibernate配置连接池(转)

来源:互联网 发布:传奇怪物数据库在哪里 编辑:程序博客网 时间:2024/05/18 16:13

 

Hibernate自带的连接池性能不高,而且还存在BUG,因此官方推荐使用c3p0或Proxool连接池。
<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>

 

原创粉丝点击