Hibernate整合连接池

本文介绍如何在Hibernate中整合C3P0连接池,包括导入jar包、配置参数等步骤,并通过示例演示了如何验证C3P0连接池的有效性。

整合c3p0(连接池)

整合c3p0:

 

步骤一:导入c3p0 jar包

 
 
 

步骤二:hibernate.cfg.xml 配置

hibernate.connection.provider_classorg.hibernate.connection.C3P0ConnectionProvider

 

c3p0具体配置参数:

 

###########################

### C3P0 Connection Pool###

###########################

 

#hibernate.c3p0.max_size 2

#hibernate.c3p0.min_size 2

#hibernate.c3p0.timeout 5000

#hibernate.c3p0.max_statements 100

#hibernate.c3p0.idle_test_period 3000

#hibernate.c3p0.acquire_increment 2

#hibernate.c3p0.validate false

 

配置:

 

<!--
	告诉Hibernate,我要使用C3p0连接池
 hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider -->
	<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 
#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false
 -->
 <property name="hibernate.c3p0.max_size">5</property>
 <property name="hibernate.c3p0.min_size">2</property>
 

 

case:

package com.hcx.e_api;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import com.hcx.domain.Customer;
import com.hcx.utils.HibernateUtils;

public class Demo {
	@Test
	//验证c3p0连接池
	public void fun1(){
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		//------------------------------------------------
		System.out.println(session);
		//------------------------------------------------
		session.getTransaction().commit();
		session.close(); // 游离状态
	}
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值