1. 程式人生 > >如何提高Hibernate 3 啟動速度

如何提高Hibernate 3 啟動速度

在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/">  
        java:comp/env/jdbc/ConfigureMeDSproperty>               
    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/">            
       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;
    }