日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Hibernate2和Hibernate3連接池配置

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

成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括南岔網(wǎng)站建設(shè)、南岔網(wǎng)站制作、南岔網(wǎng)頁制作以及南岔網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,南岔網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到南岔省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

一、Apche的DBCP在Hibernate2中受支持,但在Hibernate3中已經(jīng)不再推薦使用,官方的解釋是這個連接池存在缺陷。如果你因?yàn)槟撤N原因需要在Hibernate3中使用DBCP,建議采用JNDI方式。

二、默認(rèn)情況下(即沒有配置連接池的情況下),Hibernate會采用內(nèi)建的連接池.但這個連接池性能不佳,且存在諸多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代碼:

 
 
 
  1.  version='1.0' encoding='UTF-8'?>     
  2. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"      
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">        
  4.         
  5.  >        
  6. >        
  7.  name="connection.driver_class">com.mysql.jdbc.Driver        
  8.         
  9.  name="connection.url">          
  10. jdbc:mysql://localhost:3306/schoolproject         
  11.         
  12.  name="connection.useUnicode">true        
  13.  name="connection.characterEncoding">UTF-8        
  14.         
  15.  name="connection.username">root        
  16. >        
  17.  name="connection.password">        
  18.         
  19.  name="show_sql">true        
  20.         
  21.  name="dialect">org.hibernate.dialect.MySQLDialect        
  22.         
  23.  resource="com/wqbi/model/pojo/student.hbm.xml" />        
  24.           
  25.      
  26.  version='1.0' encoding='UTF-8'?> 
  27. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
  28. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  29.  
  30.  > 
  31. > 
  32.  name="connection.driver_class">com.mysql.jdbc.Driver 
  33.  
  34.  name="connection.url">   
  35. jdbc:mysql://localhost:3306/schoolproject  
  36.  
  37.  name="connection.useUnicode">true 
  38.  name="connection.characterEncoding">UTF-8 
  39.  
  40.  name="connection.username">root 
  41. > 
  42.  name="connection.password"> 
  43.  
  44.  name="show_sql">true 
  45.  
  46.  name="dialect">org.hibernate.dialect.MySQLDialect 
  47.  
  48.  resource="com/wqbi/model/pojo/student.hbm.xml" /> 
  49.    
  50.  

2.C3P0連接配置

Xml代碼:

 
 
 
  1.  version='1.0' encoding='UTF-8'?>        
  2. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"         
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">        
  4.         
  5.  >        
  6. >        
  7.  name="connection.driver_class">com.mysql.jdbc.Driver        
  8.         
  9.  name="connection.url">          
  10. jdbc:mysql://localhost:3306/schoolproject         
  11.         
  12.  name="connection.useUnicode">true        
  13.  name="connection.characterEncoding">UTF-8        
  14.         
  15.  name="connection.username">root        
  16.         
  17.  name="connection.password">        
  18.         
  19.  name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider     
  20.         
  21.  name="hibernate.c3p0.max_size">20        
  22.  name="hibernate.c3p0.min_size">5        
  23.  name="hibernate.c3p0.timeout">120        
  24.  name="hibernate.c3p0.max_statements">100        
  25.  name="hibernate.c3p0.idle_test_period">120        
  26.  name="hibernate.c3p0.acquire_increment">2        
  27.        
  28.  name="show_sql">true        
  29.         
  30.  name="dialect">org.hibernate.dialect.MySQLDialect        
  31.         
  32.  resource="com/wqbi/model/pojo/student.hbm.xml" />        
  33.           
  34.      
  35.  version='1.0' encoding='UTF-8'?> 
  36. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
  37. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  38.  
  39.  > 
  40. > 
  41.  name="connection.driver_class">com.mysql.jdbc.Driver 
  42.  
  43.  name="connection.url">   
  44. jdbc:mysql://localhost:3306/schoolproject  
  45.  
  46.  name="connection.useUnicode">true 
  47.  name="connection.characterEncoding">UTF-8 
  48.  
  49.  name="connection.username">root 
  50.  
  51.  name="connection.password"> 
  52.  
  53.  name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider  
  54.  
  55.  name="hibernate.c3p0.max_size">20 
  56.  name="hibernate.c3p0.min_size">5 
  57.  name="hibernate.c3p0.timeout">120 
  58.  name="hibernate.c3p0.max_statements">100 
  59.  name="hibernate.c3p0.idle_test_period">120 
  60.  name="hibernate.c3p0.acquire_increment">2 
  61.  
  62.  name="show_sql">true 
  63.  
  64.  name="dialect">org.hibernate.dialect.MySQLDialect 
  65.  
  66.  resource="com/wqbi/model/pojo/student.hbm.xml" /> 
  67.    
  68.  

3.proxool連接池

(1) 先寫proxool的配置文件,文件名:proxool.xml(一般放在與hibernate.cfg.xml文件在同一個目錄中)本例配置的是MYSQL數(shù)據(jù)庫,數(shù)據(jù)庫的名字為schoolproject

Xml代碼:

 
 
 
  1.  version="1.0" encoding="UTF-8"?>          
  2.         
  3. DBPool        
  4.         
  5.         
  6. jdbc:mysql://localhost:3306/schoolproject?useUnicode=true&characterEncoding=UTF8        
  7.         
  8. >        
  9. com.mysql.jdbc.Driver        
  10.         
  11.  name="user" value="root"/>        
  12.  name="password" value=""/>        
  13.          
  14.           
  15. 5        
  16.         
  17. 10        
  18.           
  19.      
  20.  version="1.0" encoding="UTF-8"?>   
  21.  
  22. DBPool 
  23.  
  24.  
  25. jdbc:mysql://localhost:3306/schoolproject?useUnicode=true&characterEncoding=UTF8 
  26.  
  27. > 
  28. com.mysql.jdbc.Driver 
  29.  
  30.  name="user" value="root"/> 
  31.  name="password" value=""/> 
  32.    
  33.    
  34. 5 
  35.  
  36. 10 
  37.    
  38.  

(2)配置hibernate.cfg.xml文件

Xml代碼:

 
 
 
  1.  version='1.0' encoding='UTF-8'?>        
  2. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"         
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">        
  4.         
  5.  >        
  6.  name="hibernate.connection.provider_class">        
  7. org.hibernate.connection.ProxoolConnectionProvider         
  8.         
  9.  name="hibernate.proxool.pool_alias">DBPool        
  10.  name="hibernate.proxool.xml">proxoolconf.xml        
  11.        
  12.  name="show_sql">true        
  13.         
  14.  name="dialect">org.hibernate.dialect.MySQLDialect       
  15.         
  16.  resource="com/wqbi/model/pojo/student.hbm.xml" />       
  17.           
  18.      
  19.  version='1.0' encoding='UTF-8'?> 
  20. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
  21. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  22.  
  23.  > 
  24.  name="hibernate.connection.provider_class"> 
  25. org.hibernate.connection.ProxoolConnectionProvider  
  26.  
  27.  name="hibernate.proxool.pool_alias">DBPool 
  28.  name="hibernate.proxool.xml">proxoolconf.xml 
  29.  
  30.  name="show_sql">true 
  31.  
  32.  name="dialect">org.hibernate.dialect.MySQLDialect 
  33.  
  34.  resource="com/wqbi/model/pojo/student.hbm.xml" /> 
  35.    
  36.  

(1) hibernate.connection.provider_class定義Hibernate的連接加載類,這里Proxool連接池是用這個,不同的連接池有不同的加載類,可以查閱Hibernate文檔獲取相關(guān)信息

(2) hibernate.proxool.pool_alias這里就是用我們上面提到的連接池的別名

(3) hibernate.proxool.xml是向Hibernate聲明連接池的配置文件位置,可以用相對或絕對路徑,用相對路徑時(shí)要注意一定在要Path范圍內(nèi)!不然會拋出異常.

(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ù)器將連接池對外顯示為 JNDI綁定數(shù)據(jù)源,它是javax.jdbc.Datasource類的一個實(shí)例.只要配置一個Hibernate文件,如:

 
 
 
  1. hibernate.connection.datasource=java:/comp/env/jdbc/schoolproject //JNDI名   
  2. hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory   
  3. hibernate.transaction.manager_loopup_class =   
  4. org.hibernate.transaction.JBossTransactionManagerLookup   
  5. hibernate.dialect=org.hibernate.dialect.MySQLDialect  

網(wǎng)頁標(biāo)題:Hibernate2和Hibernate3連接池配置
文章位置:http://www.dlmjj.cn/article/dpgsoii.html