hibernate學(xué)習(xí) C3P0連接池

經(jīng)過前面的學(xué)習(xí),已經(jīng)掌握了hibernate的基本使用。本篇講的是hibernate使用數(shù)據(jù)庫連接池的配置。

1、使用C3P0

  • 導(dǎo)入jar包,hibernate-release-5.2.10.Final\lib\optional\c3p0文件夾下
  • 配置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">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hiber</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!-- 綁定本地session 放在本地線程中 共三個(gè)值 一般用thread與本地線程綁定 -->
        <property name="hibernate.current_session_context_class">thread</property>
        
        <!-- C3P0連接池配置 -->
        <!-- 丟棄 不必配置即可使用 property name="hibernate.connection.provider_class">
            org.hibernate.c3p0.internal.C3P0ConnectionProvider
        </property -->
        <!-- 最大連接數(shù) -->        
        <property name="hibernate.c3p0.max_size">20</property>
        <!-- 最小連接數(shù) -->
        <property name="hibernate.c3p0.min_size">5</property>
        <!-- 當(dāng)連接池里面的連接用完的時(shí)候,C3P0一下獲取的新的連接數(shù) -->
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <!-- 最大的PreparedStatement的數(shù)量 -->
        <property name="hibernate.c3p0.max_statements">50</property>
        <!-- 連接的最大空閑時(shí)間,1800秒內(nèi)未使用則連接被丟棄。單位為秒 -->
        <property name="hibernate.c3p0.timeout">1800</property>
        <!-- 每隔120秒檢查連接池里的空閑連接是否超時(shí),單位是秒 -->
        <property name="hibernate.c3p0.idle_test_period">120</property>
        
        <mapping resource="cn/lkangle/entity/onetwo.hbm.xml"/>
        <mapping resource="cn/lkangle/entity/three.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
  • 測試使用C3P0

控制臺(tái)輸出如上信息說明C3P0使用成功。END


在源碼中發(fā)現(xiàn)hibernate中C3P0有效的配置也就已上六個(gè),而我百度到的還發(fā)現(xiàn)這樣一條配置<property name="hibernate.c3p0.validate">true</property>設(shè)置為true每次都檢測連接是否可用。沒有測試不知道是否有用。

下面是AvailableSettings類中關(guān)于C3P0的配置部分代碼

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // c3p0 connection pooling specific settings
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    /**
     * A setting prefix used to indicate settings that target the hibernate-c3p0 integration
     */
    String C3P0_CONFIG_PREFIX = "hibernate.c3p0";

    /**
     * Maximum size of C3P0 connection pool
     */
    String C3P0_MAX_SIZE = "hibernate.c3p0.max_size";

    /**
     * Minimum size of C3P0 connection pool
     */
    String C3P0_MIN_SIZE = "hibernate.c3p0.min_size";

    /**
     * Maximum idle time for C3P0 connection pool
     */
    String C3P0_TIMEOUT = "hibernate.c3p0.timeout";

    /**
     * Maximum size of C3P0 statement cache
     */
    String C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";

    /**
     * Number of connections acquired when pool is exhausted
     */
    String C3P0_ACQUIRE_INCREMENT = "hibernate.c3p0.acquire_increment";

    /**
     * Idle time beforeQuery a C3P0 pooled connection is validated
     */
    String C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Hibernate: 一個(gè)持久化框架 一個(gè)ORM框架 加載:根據(jù)特定的OID,把一個(gè)對象從數(shù)據(jù)庫加載到內(nèi)存中OID...
    JHMichael閱讀 2,089評論 0 27
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評論 19 139
  • 目錄 1. Hibernate框架的概述 1.1 Hibernate簡介 1.2 為什么要學(xué)習(xí)Hibernate ...
    深海魚Q閱讀 1,118評論 0 14
  • 一、Hibernate查詢 查詢概述Get/load主鍵查詢對象導(dǎo)航查詢HQL查詢 : Hibernate Qu...
    奮斗的老王閱讀 931評論 0 51
  • 我真的要發(fā)財(cái)了。 5年前,我看著QQ郵件里發(fā)來的“恭喜您已通過DNF記者團(tuán)寫手考核”郵件,內(nèi)心無線憧憬,想象著從此...
    赤壁傷痕閱讀 297評論 0 0

友情鏈接更多精彩內(nèi)容