隨筆 - 19, 文章 - 93, 評論 - 17, 引用 - 0
          數據加載中……

          Weblogic 8.1與Hibernate的結合的解決方案

          Weblogic 8.1與Hibernate的結合的解決方案?? - 2004-10-02? 08:17?

          ??? 基于Hibernate在O/R Mapping方面的優勢,目前項目中采用Hibernate實體替代EJB EntityBean, 本人在WebLogic 8.1的環境下做了一個測試,用EJB SessionBean調用Hibernate的數據實體。因為Weblogic和Hibernate都提供了數據庫連接池,JNDI,事務等功能。主導思想還是想利用Weblogic Server的在這些服務上的高性能管理。

          設計思想:
          ??? 使用WebLogic的數據庫連接池,而不是Hibernate自帶的連接池。
          ??? 將Hibernate的SessionFactory配置到Weblogic JNDI目錄樹下。
          ??? 在SessionBean中直接調用Hibernate的實體訪問數據庫

          準備條件:
          1、安裝以下軟件(都可以免費下載使用)
          1.1 Mysql 4.0.21 c:\mysql
          ??? 創建數據庫study,創建數據表cat
          1.2 mysql-connector-java-3.0.15-ga.zip mysql驅動程序
          1.3 Weblogic platform 8.1??? c:\bea
          ??? Weblogic配置完成,域mydomain和服務器myserver,數據池studyjndi,數據源名稱mysqldatasource
          1.4 Hibernate 2.1.2
          ??? 參考其它文檔編寫一個hibernate的實例cat,編寫Cat.hbm.xml和hibernate.cfg.xml文件,了解hibernate的基本配置。
          ??? 注意數據庫的差異。

          2.創建目錄結構
          C:\Test\lib 將hibernate解壓后lib目錄下的全部文件拷貝到此
          C:\Test\src\com\chenm 源代碼存放地(*.java)
          C:\Test\classes 將hibernate的配置文件(hibernate.properties,log4j.properties,cache.ccf)
          C:\Test\classes\com\chenm 編譯好的代碼(*.class) + Cat.hbm.xml + hibernate.cfg.xml

          步驟1:配置hibernate的環境目錄到Weblogic的CLASSPATH中。
          ??? 修改Weblogic啟動腳本C:\bea\user_projects\domains\mydomain\startweblogic.cmd,在@REM Call WebLogic Server前加入
          ??? @rem set hibernate classpath
          ??? set HIBERNATE_LIB=C:\Test\lib
          ??? set HIBERNATE_CLASSES=C:\Test\classes
          ??? SET CLASSPATH=%HIBERNATE_LIB%\cglib-2.0-rc2.jar;%HIBERNATE_LIB%\commons-collections-2.1.jar;%HIBERNATE_LIB%\commons-lang-1.0.1.jar;%HIBERNATE_LIB%\commons-logging-1.0.3.jar;%HIBERNATE_LIB%\dom4j-1.4.jar;%HIBERNATE_LIB%\hibernate2.jar;%HIBERNATE_LIB%\jcs-1.0-dev.jar;%HIBERNATE_LIB%\log4j-1.2.8.jar;%HIBERNATE_LIB%\odmg-3.0.jar;%HIBERNATE_CLASSES%;%CLASSPATH%

          步驟2:修改hibernat.properties文件
          2.1 修改以下內容
          ??? 注釋掉mysql缺省數據庫連接
          ??? ## HypersonicSQL

          ??? #hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
          ??? #hibernate.connection.driver_class org.hsqldb.jdbcDriver
          ??? #hibernate.connection.username sa
          ??? #hibernate.connection.password
          ??? #hibernate.connection.url jdbc:hsqldb:hsql://localhost
          ??? #hibernate.connection.url jdbc:hsqldb:test
          ??? #hibernate.connection.url jdbc:hsqldb:.
          ???
          ??? 使用mysql數據庫
          ??? ## MySQL

          ??? hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
          ??? #hibernate.connection.driver_class org.gjt.mm.mysql.Driver
          ??? hibernate.connection.driver_class com.mysql.jdbc.Driver
          ??? hibernate.connection.url jdbc:mysql://localhost:3306/study
          ??? hibernate.connection.username test
          ??? hibernate.connection.password weblogic
          ???
          ??? 調整數據庫查詢和插入的性能參數
          ??? 修改hibernate.jdbc.fetch_size 50
          ??? 修改hibernate.jdbc.batch_size 25
          ???
          ??? 調整Transaction API
          ??? #hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory
          ??? #hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory
          ??? 為
          ??? hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory
          ??? hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory

          ??? 使用JCS緩存
          ??? hibernate.transaction.manager_lookup_class net.sf.hibernate.transaction.WeblogicTransactionManagerLookup

          2.2 在文件尾增加以下內容
          ??? hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
          ??? hibernate.connection.datasource studyjndi // 此處為weblogic的數據連接池JNDI名稱
          ??? hibernate.connection.provider_class net.sf.hibernate.connection.DatasourceConnectionProvider
          ??? hibernate.session_factory_name hibernate.session_factory // 綁定到weblogic JNDI目錄樹中的名稱

          步驟3. 實現SessionFactory的預創建,使用Weblogic的T3StartUpDef接口創建一個StartUp類,配置成Weblogic
          啟動時自動運行。
          3.1 創建文件HibernateStartUp.java,并編譯成C:\Test\classes\com\chenm\HibernateStartUp.class文件,
          package com.chenm;

          import java.util.Hashtable;
          import weblogic.common.T3StartupDef;
          import weblogic.common.T3ServicesDef;
          import net.sf.hibernate.cfg.Configuration;
          import net.sf.hibernate.SessionFactory;

          public class HibernateStartUp implements T3StartupDef {
          ??????? public void setServices(T3ServicesDef services) {}

          ??????? public String startup(String name,Hashtable args) throws Exception {
          ??????????????? Configuration conf = new Configuration().addClass(Cat.class);
          ??????????????? SessionFactory sf = conf.buildSessionFactory();
          ??????????????? return "Hibernate Startup completed successfully";
          ??????? }
          }

          3.2 配置StartUp類
          ??? 啟動Weblogic控制臺,打開左邊mydomain\部署\啟動和關閉節點,選擇右邊"配置新的 Startup Class..."
          ??? 填寫名稱HibernateStartup, 類名com.chenm.HibernateStartUp,然后點擊"創建", 如果沒有出錯信息就算成功。
          ???
          ??? 確認成功:關閉Weblogic并重啟,觀察DOS窗口的信息,可以看到在Weblogic啟動后顯示很多行INFO,如果沒有
          ??? 錯誤,證明配置成功。再打開weblogic控制臺,選擇mydomain\服務器\myserver,點右鍵,選擇察看JNDI樹,如果
          ??? 看到Hibernate的JNDI對象,在右邊可以看見以下信息:

          ??? 綁定名稱: session_factory
          ??? 對象類: net.sf.hibernate.impl.SessionFactoryImpl
          ??? 對象散列代碼: 45706641
          ??? 對象轉換成字符串: net.sf.hibernate.impl.SessionFactoryImpl@2b96d91

          ??? Config OK!

          4. 編寫SessionBean操作Hibernate實體
          ?? 在SessionBean中定義Remote方法
          ???? public void InsertCat(String cat_id,String name, char sex, float weight) {
          ??? /**@todo Complete this method*/
          ??? try {

          Context ctx = getInitialContext();
          SessionFactory sf = (SessionFactory)ctx.lookup("hibernate/session_factory");
          Session s = sf.openSession() ;
          Transaction t = s.beginTransaction() ;

          Cat myCat = new Cat();
          myCat.setId(cat_id);
          myCat.setName(name);
          myCat.setSex(sex);
          myCat.setWeight(weight);s.save(myCat);
          s.save(myCat);
          t.commit() ;
          s.close();
            }
            catch( Exception ex ) {
            }

          ? }
          ? private Context getInitialContext() throws Exception {
          ??? String url = "t3://chenming:7001"; // chenming服務器名稱
          ??? String user = null;
          ??? String password = null;
          ??? Properties properties = null;
          ??? try {
          ????? properties = new Properties();
          ????? properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          ????? properties.put(Context.PROVIDER_URL, url);
          ????? if (user != null) {
          ??????? properties.put(Context.SECURITY_PRINCIPAL, user);
          ??????? properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
          ????? }
          ????? return new InitialContext(properties);
          ??? }
          ??? catch(Exception e) {
          ????? throw e;
          ??? }
          ? }
          ? 編寫測試并運行,在cat表中插入一條紀錄
          ? Context context = getInitialContext();

          ? //look up jndi name
          ? Object ref = context.lookup("CatSession");
          ? //look up jndi name and cast to Home interface
          ? catSessionHome = (CatSessionHome) PortableRemoteObject.narrow(ref, CatSessionHome.class);
          ? catSession = catSessionHome.create();
          ? catSession.InsertCat("007","Chenm.cat",'1',100);

          posted on 2006-04-20 17:00 BPM 閱讀(291) 評論(0)  編輯  收藏 所屬分類: Hibernate


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 双牌县| 普宁市| 常德市| 遂宁市| 浦东新区| 连城县| 宁远县| 湖口县| 宁阳县| 古丈县| 瑞安市| 枣强县| 报价| 阿克陶县| 长岛县| 读书| 铁岭县| 昭觉县| 平泉县| 德江县| 达日县| 普兰店市| 双峰县| 黄平县| 托克逊县| 张家口市| 乐平市| 余干县| 巫溪县| 长垣县| 凤庆县| 长白| 观塘区| 于都县| 秦安县| 佛冈县| 武隆县| 兰考县| 灵川县| 额尔古纳市| 巫溪县|