提高Hibernate 3 启动速度(翻译)

原文地址:http://www.hibernate.org/300.html

在Tomcat5.5.x环境下,调用Configuration().addCacheableFile来载入配置,建立Hibernate SessionFactory,成功地提高了载入速度。

推荐你只是在开发阶段采用这样的方式载入,最后的产品发布阶段你仍需使用经典的Hibernate.cfg.xml文件,通过Tomcat的ServletContextListener API在应用程序部署的时候建立Hibernate SessionFactory,而不是在程序第一次调用Hiberante的时候。

文件:

net/netbauds/catalina/IHibernateCachableFileLoad.java

这个文件可以在不同的web应用中使用而不用作任何修改。

package  net.netbauds.catalina;

import  org.hibernate.cfg.Configuration;

public   interface  IHibernateCachableFileLoad {

    
public   void  addMappings(Configuration conf);

}

net/netbauds/catalina/HibernateSessionFactory.java

使用静态方法HibernateSessionFactory.getSessionFactory() 来代替我们以前使用的Configuration().configure().buildSessionFactory(),这个方法一般在你的HibernateSession单态类中(参考http://www.hibernate.org/114.html)。

这个文件也可以在不同的应用中使用而不加任何修改:

package  net.netbauds.catalina;

import  org.hibernate.SessionFactory;
import  org.hibernate.cfg.Configuration;

//  单态的 sessionFactory
public   class  HibernateSessionFactory {
    
private   static  SessionFactory sessionFactory;

    
public   static  SessionFactory getSessionFactory() {
        
//  不要从 JNDI中获取SessionFactory, 使用一个静态的 SessionFactory
         if  (sessionFactory  ==   null ) {
            Configuration conf 
=   new  Configuration();

            
try  {

                Class klass 
=  Class.forName( " config.HibernateCachableFileLoad " );

                IHibernateCachableFileLoad hibConf 
=  (IHibernateCachableFileLoad) klass.newInstance();

                hibConf.addMappings(conf);

            } 
catch  (ClassNotFoundException e) {
                
//  NOOP
            }  catch  (InstantiationException e) {
                
//  NOOP
            }  catch  (IllegalAccessException e) {
                
//  NOOP
            }
 
            Configuration confdone 
=  conf.configure();

            
if (confdone  !=   null ) {
                
//  Use default hibernate.cfg.xml
                sessionFactory  =  confdone.buildSessionFactory();
            }
        }

        
return  sessionFactory;
    }
}


config/HibernateCachableFileLoad.java

这个文件是随web应用的不同而不同的,你需要修改代码中的某些部分使其适合你的应用。应该有人知道如何更容易的由class loader获得WEB-INF/classes的绝对路径吧,这里我只是把它直接写入了程序中。

你需要修改如下部分:

* 将你所有的Hibernate映射配置文件(*.hbm.xml)加入到程序中(正如你在Hibernate.cfg.xml中所做的)。

package  config;

import  net.netbauds.catalina.IHibernateCachableFileLoad;
import  org.hibernate.cfg.Configuration;

//  This class is webapp specific and allow loading of mapping via
//   addCachableFile();
public   class  HibernateCachableFileLoad  implements  IHibernateCachableFileLoad {

    
public   void  addMappings(Configuration conf) {

        doFile(conf, 
" com/mydomain/MyClassFile001.hbm.xml " );

        doFile(conf, 
" com/mydomain/MyClassFile002.hbm.xml " );

    }

    
private   void  doFile(Configuration conf, String resPath) {

        String path 
=   null ;

        URL u 
=   this .getClass().getClassLoader().getResource(resPath);

        
if (u  !=   null ) {

            path 
=  u.getFile();
            
if (path  !=   null )
                conf 
=  conf.addCacheableFile(path);
        }

        
if (path  ==   null   ||  conf  ==   null )
            System.err.println(
" ERROR: Failed to load:  "   +  resPath);
    }
}

hibernate.cfg.xml

这将使我们标准的hibernate.cfg.xml发生一些变化。如果你使用的是hibernate-configuration-3.0.dtd(3.0版本),那么你可以不写入任何mapping元素。

如果你使用的是老版本的dtd,那么你需要在hibernate.cfg.xml中写入一个mapping元素。

An alternative way maybe to programatically configure the connection.datasource in the HibernateSessionFactory() above and maybe hibernate will allow you to do away with looking and parsing the hibernate.cfg.xml completely and build a working factory with the Configuration you have programatically created.

一个可供选择的方法是使用编写java代码的方式来配置上面的SessionFactory的connection.datasource,也许Hibernate将允许你读取hibernate.cfg.xml文件并且是用你在程序中创建的Configuration来建立一个sessionFactory。

你需要作如下修改:

* 将 java:comp/env/jdbc/ConfigureMeDS 修改为你自己的数据库连接信息

那么现在:

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

< hibernate-configuration >
    
< session-factory >
        
< property  name ="connection.datasource" > java:comp/env/jdbc/ConfigureMeDS </ property >

        
<!--  Mapping files  -->
        
<!--  <mapping resource="uk/mydomain/Dummy.hbm.xml"/>  -->
    
</ session-factory >
</ hibernate-configuration >

如果你使用的Hibernate版本需要在hibernate.cfg.xml中至少有一个mapping元素,那么你可能需要用到下面的两个文件,否则,如上就可以了。

uk/mydomain/Dummy.hbm.xml

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping >
    
< class  name ="uk.mydomain.Dummy"  table ="dummy" >
       
< id  name ="id"  type ="long"  column ="id" >
           
< generator  class ="native"   />
       
</ id >
    
</ class >
</ hibernate-mapping >

uk/mydomain/Dummy.java

package uk.mydomain;

public class Dummy {
    private long id;
    private long getId() {
        return id;
    }

    private void setId(long id) {
        this.id = id;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值