(16)Hibernate对连接池的支持

除非我们用爱去对待一个人,否则我们无法了解他。 

创新互联公司是一家专业提供善右企业网站建设,专注与成都网站设计、做网站、H5页面制作、小程序制作等业务。10年已为善右众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

We never can have a true view of man unless we have a love for him.

1、连接池知识

连接池的作用: 管理连接;提升连接的利用效率!

常用的连接池: C3P0连接池

Hibernate 自带的也有一个连接池,且对C3P0连接池也有支持!

Hibernate自带连接池:只维护一个连接,比较简陋。

可以查看hibernate.properties文件(%hibernate%/project/etc/hibernate.properties)

Hibernate对C3P0连接池支持的核心类是org.hibernate.connection.C3P0ConnectionProvider

告诉Hibernate使用的是哪一个连接池技术。

#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider

hibernate.properties中连接池详细配置:

#################################
### Hibernate Connection Pool ###
#################################

hibernate.connection.pool_size 1            #Hibernate自带连接池:只有一个连接



###########################
### C3P0 Connection Pool###                 #Hibernate对C3P0连接池支持
###########################

#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              #

#################################
### Plugin ConnectionProvider ###
#################################

## use a custom ConnectionProvider (if not set, Hibernate will choose a built-in ConnectionProvider using hueristics)

#hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider
#hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider
#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider
#hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider

2、使用连接池的步骤

(1)添加C3P0的jar包

    %hibernate%/lib/optional/c3p0/c3p0-0.9.1.jar

(2)在hibernate.cfg.xml文件中添加数据库连接池配置

    核心配置

		<!--****************** 【连接池配置】****************** -->
		<!-- 配置连接驱动管理类 -->		
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!-- 配置连接池参数信息 -->
		<property name="hibernate.c3p0.min_size">4</property>
		<property name="hibernate.c3p0.max_size">8</property>
		<property name="hibernate.c3p0.timeout">5000</property>
		<property name="hibernate.c3p0.max_statements">10</property>
		<property name="hibernate.c3p0.idle_test_period">10000</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>

    完整配置

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <!-- 通常,一个session-factory节点代表一个数据库 -->
    <session-factory>
        <!-- 1. 数据库连接配置 -->
        <property name="hibernate.connection.driver_class">com.MySQL.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///test</property>	
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
		<!-- 
			数据库方言配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
		 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        
        <!-- 2. 其他相关配置 -->
		<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 2.2 格式化sql -->
		<property name="hibernate.format_sql">false</property>
		<!-- 2.3 自动建表  -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!--****************** 【连接池配置】****************** -->
		<!-- 配置连接驱动管理类 -->		
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!-- 配置连接池参数信息 -->
		<property name="hibernate.c3p0.min_size">4</property>
		<property name="hibernate.c3p0.max_size">8</property>
		<property name="hibernate.c3p0.timeout">5000</property>
		<property name="hibernate.c3p0.max_statements">10</property>
		<property name="hibernate.c3p0.idle_test_period">10000</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>
		
		<!-- 3. 加载所有映射-->
		<!-- <mapping resource="com/rk/hibernate/a_hello/Employee.hbm.xml"/> -->
		 
    </session-factory>
</hibernate-configuration>

(3)使用SHOW PROCESSLIST;进行测试

    测试代码

	@Test
	public void testPool()
	{
		Session session = sf.openSession();
		session.beginTransaction();
		
		Department dept = (Department)session.get(Department.class, 3);
		System.out.println(dept);	//在这里打断点,并使用  SHOW PROCESSLIST;  查看活跃连接
		
		session.getTransaction().commit();
		session.close();
	}

    测试方法

    a)在执行testPool()方法之前,使用SHOW PROCESSLIST;查看活跃的连接数量

    b)调试执行testPool()方法,停在断点时,查看活跃的连接数量

    c)testPool()方法执行完之后,查看活跃连接数量

    d)修改配置中hibernate.c3p0.min_size数目,再次执行a,b,c查看活跃连接数量

文章名称:(16)Hibernate对连接池的支持
转载源于:https://www.cdcxhl.com/article18/igiggp.html

成都网站建设公司_创新互联,为您提供手机网站建设云服务器网站策划网站导航动态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司