在Hibernate中使用C3P0连接池

本文详细介绍了如何将C3P0连接池类库添加到项目构建路径下,并配置Hibernate.cfg.xml文件以实现与C3P0的集成。包括设置连接池参数如最小连接数、最大连接数等,以及修改配置文件中的其他关键链接信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:

把C3P0的类库添加到项目的构建路径下。

项目-属性-buildpath - add externaljars

c3p0-0.9.1.jar

此jar包可以在个人网盘中下载到。

2:

编辑hibernate.cfg.xml 文件


  1. SRC 2 7  hibernate.cfg.xml  
  2. <?xml version='1.0' encoding='UTF-8'?>  
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
  5.           "http://hibernate.sourceforge.net  
  6.           /hibernate-configuration-3.0.dtd">  
  7. <hibernate-configuration>  
  8.     <session-factory>  
  9. <property name="connection.driver_class">oracle.jdbc.driver.  
  10. OracleDriver</property>  
  11. <property name="connection.url">jdbc:oracle:thin:@localhost:  
  12. 1521:ora9</property>  
  13. <property name="dialect">org.hibernate.dialect.  
  14. Oracle9iDialect</property>  
  15. <property name="connection.username">scott</property>  
  16. <property name="connection.password">tiger</property>  

  17. <!-- C3P0 连接池的配置 -->  
  18. <property name="connection.provider_class">  
  19.         org.hibernate.connection.C3P0ConnectionProvider  
  20. </property>  
  21. <property name="hibernate.c3p0.min_size">5</property>  
  22. <property name="hibernate.c3p0.max_size">10</property>  
  23. <property name="hibernate.c3p0.max_statements">50</property>  
  24. <property name="hibernate.c3p0.timeout">3600</property>  
  25. <property name="hibernate.c3p0.idle_test_period">120</property>  
  26. <property name="hibernate.c3p0.acquire_increment">2</property>  
  27. <!-- 结束 -->
  28. <property name="current_session_context_class">thread</property>  
  29. <property name="show_sql">true</property>  
  30. <property name="format_sql">true</property>  
  31. <mapping resource="com/v512/examples/Guestbook.hbm.xml" />  
  32.     </session-factory>  
  33. </hibernate-configuration> 


  3:修改Hibernate.cfg.xml配置文件的其他链接信息。OK!测试一下吧。




---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

- hibernate.c3p0.min_size:设置连接池的最小连接数。

- hibernate.c3p0.max_siz:设置连接池的最大连接数。

- hibernate.c3p0.timeout:设置连接池中的连接的最大空闲时间,超时后会被删除,单位为秒。

- hibernate.c3p0.max_statements:设置连接池中Statement对象的最大数量。

- hibernate.c3p0.idle_test_period:设置检查连接池中空闲连接的间隔时间,单位为秒。

- hibernate.c3p0.acquire_increment:设置连接池的连接用完后每次新建连接的数量。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值