Weblogic 8.1與Hibernate的結(jié)合的解決方案
Weblogic 8.1與Hibernate的結(jié)合的解決方案?? - 2004-10-02? 08:17?
??? 基于Hibernate在O/R Mapping方面的優(yōu)勢,目前項(xiàng)目中采用Hibernate實(shí)體替代EJB EntityBean, 本人在WebLogic 8.1的環(huán)境下做了一個(gè)測試,用EJB SessionBean調(diào)用Hibernate的數(shù)據(jù)實(shí)體。因?yàn)閃eblogic和Hibernate都提供了數(shù)據(jù)庫連接池,JNDI,事務(wù)等功能。主導(dǎo)思想還是想利用Weblogic Server的在這些服務(wù)上的高性能管理。
設(shè)計(jì)思想:
??? 使用WebLogic的數(shù)據(jù)庫連接池,而不是Hibernate自帶的連接池。
??? 將Hibernate的SessionFactory配置到Weblogic JNDI目錄樹下。
??? 在SessionBean中直接調(diào)用Hibernate的實(shí)體訪問數(shù)據(jù)庫
準(zhǔn)備條件:
1、安裝以下軟件(都可以免費(fèi)下載使用)
1.1 Mysql 4.0.21 c:\mysql
??? 創(chuàng)建數(shù)據(jù)庫study,創(chuàng)建數(shù)據(jù)表cat
1.2 mysql-connector-java-3.0.15-ga.zip mysql驅(qū)動(dòng)程序
1.3 Weblogic platform 8.1??? c:\bea
??? Weblogic配置完成,域mydomain和服務(wù)器myserver,數(shù)據(jù)池studyjndi,數(shù)據(jù)源名稱mysqldatasource
1.4 Hibernate 2.1.2
??? 參考其它文檔編寫一個(gè)hibernate的實(shí)例cat,編寫Cat.hbm.xml和hibernate.cfg.xml文件,了解hibernate的基本配置。
??? 注意數(shù)據(jù)庫的差異。
2.創(chuàng)建目錄結(jié)構(gòu)
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的環(huán)境目錄到Weblogic的CLASSPATH中。
??? 修改Weblogic啟動(dòng)腳本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 修改以下內(nèi)容
??? 注釋掉mysql缺省數(shù)據(jù)庫連接
??? ## 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數(shù)據(jù)庫
??? ## 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
???
??? 調(diào)整數(shù)據(jù)庫查詢和插入的性能參數(shù)
??? 修改hibernate.jdbc.fetch_size 50
??? 修改hibernate.jdbc.batch_size 25
???
??? 調(diào)整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 在文件尾增加以下內(nèi)容
??? hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
??? hibernate.connection.datasource studyjndi // 此處為weblogic的數(shù)據(jù)連接池JNDI名稱
??? hibernate.connection.provider_class net.sf.hibernate.connection.DatasourceConnectionProvider
??? hibernate.session_factory_name hibernate.session_factory // 綁定到weblogic JNDI目錄樹中的名稱
步驟3. 實(shí)現(xiàn)SessionFactory的預(yù)創(chuàng)建,使用Weblogic的T3StartUpDef接口創(chuàng)建一個(gè)StartUp類,配置成Weblogic
啟動(dòng)時(shí)自動(dòng)運(yùn)行。
3.1 創(chuàng)建文件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類
??? 啟動(dòng)Weblogic控制臺(tái),打開左邊mydomain\部署\啟動(dòng)和關(guān)閉節(jié)點(diǎn),選擇右邊"配置新的 Startup Class..."
??? 填寫名稱HibernateStartup, 類名com.chenm.HibernateStartUp,然后點(diǎn)擊"創(chuàng)建", 如果沒有出錯(cuò)信息就算成功。
???
??? 確認(rèn)成功:關(guān)閉Weblogic并重啟,觀察DOS窗口的信息,可以看到在Weblogic啟動(dòng)后顯示很多行INFO,如果沒有
??? 錯(cuò)誤,證明配置成功。再打開weblogic控制臺(tái),選擇mydomain\服務(wù)器\myserver,點(diǎn)右鍵,選擇察看JNDI樹,如果
??? 看到Hibernate的JNDI對(duì)象,在右邊可以看見以下信息:
??? 綁定名稱: session_factory
??? 對(duì)象類: net.sf.hibernate.impl.SessionFactoryImpl
??? 對(duì)象散列代碼: 45706641
??? 對(duì)象轉(zhuǎn)換成字符串: net.sf.hibernate.impl.SessionFactoryImpl@2b96d91
??? Config OK!
4. 編寫SessionBean操作Hibernate實(shí)體
?? 在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服務(wù)器名稱
??? 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;
??? }
? }
? 編寫測試并運(yùn)行,在cat表中插入一條紀(jì)錄
? 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 閱讀(301) 評(píng)論(0) 編輯 收藏 所屬分類: Hibernate