新聞中心
Hibernate支持第三方的連接池,官方推薦的連接池是C3P0,Proxool,以及DBCP.在配置連接池時(shí)需要注意的有三點(diǎn):

成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括南岔網(wǎng)站建設(shè)、南岔網(wǎng)站制作、南岔網(wǎng)頁(yè)制作以及南岔網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,南岔網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到南岔省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
一、Apche的DBCP在Hibernate2中受支持,但在Hibernate3中已經(jīng)不再推薦使用,官方的解釋是這個(gè)連接池存在缺陷。如果你因?yàn)槟撤N原因需要在Hibernate3中使用DBCP,建議采用JNDI方式。
二、默認(rèn)情況下(即沒有配置連接池的情況下),Hibernate會(huì)采用內(nèi)建的連接池.但這個(gè)連接池性能不佳,且存在諸多BUG(筆者就曾在Mysql環(huán)境下被八小時(shí)連接懸空問題困擾過),因此官方也只是建議僅在開發(fā)環(huán)境下使用。
三、 Hibernate2和Hibernate3的命名空間有所變化.例如,配置C3P0時(shí)的provider_class有Hibernate2環(huán)境下使用 net.sf.hibernate.connection.C3P0ConnectionProvider,在Hibernate3環(huán)境下使用 org.hibernate.connection.C3P0ConnectionProvider。
下面是Hibernate環(huán)境下幾種常見的連接池配置:
1.Hibernate默認(rèn)連接池
Xml代碼:
- version='1.0' encoding='UTF-8'?>
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
> - >
name="connection.driver_class">com.mysql.jdbc.Driver name="connection.url"> - jdbc:mysql://localhost:3306/schoolproject
name="connection.useUnicode">true name="connection.characterEncoding">UTF-8 name="connection.username">root - >
name="connection.password"> name="show_sql">true name="dialect">org.hibernate.dialect.MySQLDialect resource="com/wqbi/model/pojo/student.hbm.xml" /> - version='1.0' encoding='UTF-8'?>
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
> - >
name="connection.driver_class">com.mysql.jdbc.Driver name="connection.url"> - jdbc:mysql://localhost:3306/schoolproject
name="connection.useUnicode">true name="connection.characterEncoding">UTF-8 name="connection.username">root - >
name="connection.password"> name="show_sql">true name="dialect">org.hibernate.dialect.MySQLDialect resource="com/wqbi/model/pojo/student.hbm.xml" />
2.C3P0連接配置
Xml代碼:
- version='1.0' encoding='UTF-8'?>
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
> - >
name="connection.driver_class">com.mysql.jdbc.Driver name="connection.url"> - jdbc:mysql://localhost:3306/schoolproject
name="connection.useUnicode">true name="connection.characterEncoding">UTF-8 name="connection.username">root name="connection.password"> name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider name="hibernate.c3p0.max_size">20 name="hibernate.c3p0.min_size">5 name="hibernate.c3p0.timeout">120 name="hibernate.c3p0.max_statements">100 name="hibernate.c3p0.idle_test_period">120 name="hibernate.c3p0.acquire_increment">2 name="show_sql">true name="dialect">org.hibernate.dialect.MySQLDialect resource="com/wqbi/model/pojo/student.hbm.xml" /> - version='1.0' encoding='UTF-8'?>
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
> - >
name="connection.driver_class">com.mysql.jdbc.Driver name="connection.url"> - jdbc:mysql://localhost:3306/schoolproject
name="connection.useUnicode">true name="connection.characterEncoding">UTF-8 name="connection.username">root name="connection.password"> name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider name="hibernate.c3p0.max_size">20 name="hibernate.c3p0.min_size">5 name="hibernate.c3p0.timeout">120 name="hibernate.c3p0.max_statements">100 name="hibernate.c3p0.idle_test_period">120 name="hibernate.c3p0.acquire_increment">2 name="show_sql">true name="dialect">org.hibernate.dialect.MySQLDialect resource="com/wqbi/model/pojo/student.hbm.xml" />
3.proxool連接池
(1) 先寫proxool的配置文件,文件名:proxool.xml(一般放在與hibernate.cfg.xml文件在同一個(gè)目錄中)本例配置的是MYSQL數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)的名字為schoolproject
Xml代碼:
- version="1.0" encoding="UTF-8"?>
DBPool - jdbc:mysql://localhost:3306/schoolproject?useUnicode=true&characterEncoding=UTF8
- >
com.mysql.jdbc.Driver name="user" value="root"/> name="password" value=""/> 5 10 - version="1.0" encoding="UTF-8"?>
DBPool - jdbc:mysql://localhost:3306/schoolproject?useUnicode=true&characterEncoding=UTF8
- >
com.mysql.jdbc.Driver name="user" value="root"/> name="password" value=""/> 5 10
(2)配置hibernate.cfg.xml文件
Xml代碼:
- version='1.0' encoding='UTF-8'?>
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
> name="hibernate.connection.provider_class"> - org.hibernate.connection.ProxoolConnectionProvider
name="hibernate.proxool.pool_alias">DBPool name="hibernate.proxool.xml">proxoolconf.xml name="show_sql">true name="dialect">org.hibernate.dialect.MySQLDialect resource="com/wqbi/model/pojo/student.hbm.xml" /> - version='1.0' encoding='UTF-8'?>
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
> name="hibernate.connection.provider_class"> - org.hibernate.connection.ProxoolConnectionProvider
name="hibernate.proxool.pool_alias">DBPool name="hibernate.proxool.xml">proxoolconf.xml name="show_sql">true name="dialect">org.hibernate.dialect.MySQLDialect resource="com/wqbi/model/pojo/student.hbm.xml" />
(1) hibernate.connection.provider_class定義Hibernate的連接加載類,這里Proxool連接池是用這個(gè),不同的連接池有不同的加載類,可以查閱Hibernate文檔獲取相關(guān)信息
(2) hibernate.proxool.pool_alias這里就是用我們上面提到的連接池的別名
(3) hibernate.proxool.xml是向Hibernate聲明連接池的配置文件位置,可以用相對(duì)或絕對(duì)路徑,用相對(duì)路徑時(shí)要注意一定在要Path范圍內(nèi)!不然會(huì)拋出異常.
(4) dialect是聲明sql語句的方言
(5) show_sql定義是否顯示Hibernate生成的sql語言,一般在調(diào)試階段設(shè)為true,完成后再改成false,這樣有利于調(diào)試.
(6)
4.JNDI 連接池
數(shù)據(jù)源已經(jīng)由應(yīng)用服務(wù)配置好(如Web服務(wù)器),Hibernate需要做的只是通過JNDI名查找到此數(shù)據(jù)源.應(yīng)用服務(wù)器將連接池對(duì)外顯示為 JNDI綁定數(shù)據(jù)源,它是javax.jdbc.Datasource類的一個(gè)實(shí)例.只要配置一個(gè)Hibernate文件,如:
- hibernate.connection.datasource=java:/comp/env/jdbc/schoolproject //JNDI名
- hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory
- hibernate.transaction.manager_loopup_class =
- org.hibernate.transaction.JBossTransactionManagerLookup
- hibernate.dialect=org.hibernate.dialect.MySQLDialect
本文名稱:Hibernate2和Hibernate3連接池配置
分享地址:http://fisionsoft.com.cn/article/dpgsoii.html


咨詢
建站咨詢
