(16)Hibernate對串連池的支援

來源:互聯網
上載者:User

標籤:hibernate

除非我們用愛去對待一個人,否則我們無法瞭解他。 

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;進行測試

    測試代碼

@Testpublic 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對串連池的支援

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.