Weblogic 8.1与Hibernate的结合的解决方案

类别:Java 点击:0 评论:0 推荐:
版权声明:方便学习使用,本文可以任意转载

    基于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);

联系方式:QQ:77322113

本文地址:http://com.8s8s.com/it/it15058.htm