除非我們用愛去對待一個(gè)人,否則我們無法了解他。 We never can have a true view of man unless we have a love for him. |
1、連接池知識(shí)
連接池的作用: 管理連接;提升連接的利用效率!
常用的連接池: C3P0連接池
Hibernate 自帶的也有一個(gè)連接池,且對C3P0連接池也有支持!
Hibernate自帶連接池:只維護(hù)一個(gè)連接,比較簡陋。
可以查看hibernate.properties文件(%hibernate%/project/etc/hibernate.properties)
Hibernate對C3P0連接池支持的核心類是org.hibernate.connection.C3P0ConnectionProvider
告訴Hibernate使用的是哪一個(gè)連接池技術(shù)。
#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider
hibernate.properties中連接池詳細(xì)配置:
################################# ### Hibernate Connection Pool ### ################################# hibernate.connection.pool_size 1 #Hibernate自帶連接池:只有一個(gè)連接 ########################### ### C3P0 Connection Pool### #Hibernate對C3P0連接池支持 ########################### #hibernate.c3p0.max_size 2 #大連接數(shù) #hibernate.c3p0.min_size 2 #最小連接數(shù) #hibernate.c3p0.timeout 5000 #超時(shí)時(shí)間 #hibernate.c3p0.max_statements 100 #大執(zhí)行的命令的個(gè)數(shù) #hibernate.c3p0.idle_test_period 3000 #空閑測試時(shí)間 #hibernate.c3p0.acquire_increment 2 #連接不夠用的時(shí)候, 每次增加的連接數(shù) #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.ProxoolConnectionProvider2、使用連接池的步驟
(1)添加C3P0的jar包
%hibernate%/lib/optional/c3p0/c3p0-0.9.1.jar
(2)在hibernate.cfg.xml文件中添加數(shù)據(jù)庫連接池配置
核心配置
<!--****************** 【連接池配置】****************** --> <!-- 配置連接驅(qū)動(dòng)管理類 --> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!-- 配置連接池參數(shù)信息 --> <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> <!-- 通常,一個(gè)session-factory節(jié)點(diǎn)代表一個(gè)數(shù)據(jù)庫 --> <session-factory> <!-- 1. 數(shù)據(jù)庫連接配置 --> <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> <!-- 數(shù)據(jù)庫方言配置, hibernate在運(yùn)行的時(shí)候,會(huì)根據(jù)不同的方言生成符合當(dāng)前數(shù)據(jù)庫語法的sql --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2. 其他相關(guān)配置 --> <!-- 2.1 顯示hibernate在運(yùn)行時(shí)候執(zhí)行的sql語句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql --> <property name="hibernate.format_sql">false</property> <!-- 2.3 自動(dòng)建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!--****************** 【連接池配置】****************** --> <!-- 配置連接驅(qū)動(dòng)管理類 --> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!-- 配置連接池參數(shù)信息 --> <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;進(jìn)行測試
測試代碼
@Test public void testPool() { Session session = sf.openSession(); session.beginTransaction(); Department dept = (Department)session.get(Department.class, 3); System.out.println(dept); //在這里打斷點(diǎn),并使用 SHOW PROCESSLIST; 查看活躍連接 session.getTransaction().commit(); session.close(); }測試方法
a)在執(zhí)行testPool()方法之前,使用SHOW PROCESSLIST;查看活躍的連接數(shù)量
b)調(diào)試執(zhí)行testPool()方法,停在斷點(diǎn)時(shí),查看活躍的連接數(shù)量
c)testPool()方法執(zhí)行完之后,查看活躍連接數(shù)量
d)修改配置中hibernate.c3p0.min_size數(shù)目,再次執(zhí)行a,b,c查看活躍連接數(shù)量
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站名稱:(16)Hibernate對連接池的支持-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article36/dsjhsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、企業(yè)建站、App開發(fā)、ChatGPT、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)