hibernate+proxool的資料庫連接池配置方法
來源:互聯網
上載者:User
使用Hibernate 3.0做資料執久層解決方案時,怎麼配置Proxool 0.8.3資料庫連接池的方法。 1、將proxool-0.8.3.jar以及串連資料庫用到的jar檔案包含在你的項目的classpath或者WEB-INF/lib下面,本例使用的是MS Sqlserver資料庫。 2、在和hibernate.cfg.xml平級的目錄建立一個proxoolconf.xml檔案,添加以下檔案內容: <?xml version="1.0" encoding="utf-8"?> <something-else-entirely> <proxool> <alias>proxoolPool</alias> <!--proxool只能管理由自己產生的串連--> <driver-url>jdbc:microsoft:sqlserver://192.168.0.5:1433;DatabaseName=videopublish</driver-url> <driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class> <driver-properties> <property name="user" value="jdbc_user"/> <property name="password" value="memory"/> </driver-properties> <!-- proxool自動偵察各個串連狀態的時間間隔(毫秒),偵察到閒置串連就馬上回收,逾時的銷毀--> <house-keeping-sleep-time>90000</house-keeping-sleep-time> <!-- 指因未有空閑串連可以分配而在隊列中等候的最大請求數,超過這個請求數的使用者串連就不會被接受--> <maximum-new-connections>20</maximum-new-connections> <!-- 最少保持的空閑串連數--> <prototype-count>5</prototype-count> <!-- 允許最大串連數,超過了這個串連,再有請求時,就排在隊列中等候,最大的等待請求數由maximum-new-connections決定--> <maximum-connection-count>100</maximum-connection-count> <!-- 最小串連數--> <minimum-connection-count>10</minimum-connection-count> </proxool> </something-else-entirely> 3、以上就是proxool所需的設定檔,下面就是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="proxool.pool_alias">proxoolPool</property> <property name="proxool.xml">proxoolconf.xml</property> <property name="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property> <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property> <property name="show_sql">true</property> <mapping resource="com/vp/persistent/pojo/Users.hbm.xml" /> <mapping resource="com/vp/persistent/pojo/Videoclasses.hbm.xml" /> <mapping resource="com/vp/persistent/pojo/Videodiscuss.hbm.xml" /> <mapping resource="com/vp/persistent/pojo/Videoinfo.hbm.xml" /> </session-factory> </hibernate-configuration> 4、以上就是配置hibernate+proxool作為資料庫連接池的解決方案的代碼,本例提供了一個簡單的測試代碼,如下: <% long r=0; for(int i=0;i<10000;i++){ try{ long o=new Date().getTime(); Session s=HSF.currentSession(); Users u=new Users(); u.setUsername("zhanglili"); u.setUseremail("baidongli@gmail.com"); u.setUsernickname("白冬立"); u.setUserpassword("1234567980123456789"); u.setUsersex("男"); u.setUsersigndate(new Date()); Transaction tt=s.beginTransaction(); s.save(u); tt.commit(); Users user=(Users)s.get(Users.class,new Integer(1)); //out.println(user.getUsername()); long n=new Date().getTime(); r+=n-o; }catch(Exception e){ out.println(e.getMessage()); }finally{ HSF.closeSession(); } } out.println("avg="+r/10); %> 5、說明一下,我分別用proxool、tomcat JNDI、Hibernate內建的串連池進行配置之後分別執行以上的10000條資料插入和讀取,結果顯示proxool的效能要優於tomcat JNDI,而Hibernate內建的就更不用提了,差很遠。依次為:39265毫秒/10000條、26013毫秒/10000條、50029毫秒/10000條。 6、在Hibernate配置串連池時還有一些解決方案,用的比較多的例如:c3p0,apache dhcp,但網上許多評論顯示c3p0在大負荷的負載情況下,效能會降低許多,apache dhcp因為Hibernate作者在測試時發現了效能Bug,所以在Hibernate3.0版本中已經去掉了對其的支援,相比之下,proxool是大家都推薦使用的,許多業界成熟的公司(例如三星)都採用proxool做為方案。 7、厚厚,最後,如果有交流struts,hibernate,spring,freemarker,velocity,以及prototype,scriptaculous等ajax架構的朋友,可以加我的msn:baidongli888@msn.com 2006.10.31