如何在Hibernate中配置C3P0连接池

连接池
连接池提高了性能,因为它防止Java应用程序每次与数据库交互时创建连接,并最大程度地减少了打开和关闭连接的成本。

请参阅Wiki连接池说明

Hibernate带有内部连接池,但不适合生产使用。 在本教程中,我们向您展示如何将第三方连接池– C3P0与Hibernate集成。

1.获取hibernate-c3p0.jar

要将c3p0与Hibernate集成,您需要hibernate-c3p0.jar ,可从JBoss存储库中获取它。

档案:pom.xml

<project ...>

	<repositories>
		<repository>
			<id>JBoss repository</id>
			<url>http://repository.jboss.org/nexus/content/groups/public/</url>
		</repository>
	</repositories>

	<dependencies>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.6.3.Final</version>
		</dependency>
		
		<!-- Hibernate c3p0 connection pool -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>3.6.3.Final</version>
		</dependency>

	</dependencies>
</project>

2.配置c3p0属性

要配置c3p0,请将c3p0配置详细信息放在“ hibernate.cfg.xml ”中,如下所示:

档案:hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:MKYONG</property>
  <property name="hibernate.connection.username">mkyong</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">MKYONG</property>
  <property name="show_sql">true</property>
  
  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">20</property>
  <property name="hibernate.c3p0.timeout">300</property>
  <property name="hibernate.c3p0.max_statements">50</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>
        
  <mapping class="com.mkyong.user.DBUser"></mapping>
</session-factory>
</hibernate-configuration>
  1. hibernate.c3p0.min_size –池中最小的JDBC连接数。 休眠默认值:1
  2. hibernate.c3p0.max_size –池中的最大JDBC连接数。 休眠默认值:100
  3. hibernate.c3p0.timeout –从池中删除空闲连接时(秒)。 休眠默认值:0,永不过期。
  4. hibernate.c3p0.max_statements –准备的语句数将被缓存。 提高性能。 休眠默认值:0,禁用缓存。
  5. hibernate.c3p0.idle_test_period –自动验证连接之前的空闲时间(以秒为单位)。 休眠默认值:0

注意
有关休眠-C3P0配置设置的细节,请阅读文章。

运行它,输出

完成,运行它,然后看到以下输出:

c3p0 connection pool in hibernate

在连接初始化过程中,将在连接池中创建5个数据库连接,以供Web应用程序重新使用。

下载它– Hibernate-C3P0-Connection-Pool-Example.zip (8KB)

参考

  1. http://docs.jboss.org/hibernate/core/3.6/reference/zh-CN/html_single/#d0e1748
  2. http://www.mchange.com/projects/c3p0/index.html#appendix_d

翻译自: https://mkyong.com/hibernate/how-to-configure-the-c3p0-connection-pool-in-hibernate/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值