java

位置:IT落伍者 >> java >> 浏览文章

如何提高Hibernate 3 启动速度


发布日期:2021年07月21日
 
如何提高Hibernate 3 启动速度
在Tomcatx环境下调用Configuration()addCacheableFile来载入配置建立Hibernate SessionFactory成功地提高了载入速度

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


文件

net/netbauds/catalina/IHibernateCachableFileLoadjava

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

package baudscatalina;

import orghibernatecfgConfiguration;

public interface IHibernateCachableFileLoad{

public void addMappings(Configurationconf);

}

net/netbauds/catalina/HibernateSessionFactoryjava

使用静态方法HibernateSessionFactorygetSessionFactory() 来代替我们以前使用的Configuration(nfigure()buildSessionFactory()这个方法一般在你的HibernateSession单态类中(参考)

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

package baudscatalina;

import orghibernateSessionFactory;

import orghibernatecfgConfiguration;

// 单态的sessionFactory

public class HibernateSessionFactory{

private static SessionFactorysessionFactory;

public static SessionFactorygetSessionFactory(){

// 不要从JNDI中获取SessionFactory使用一个静态的SessionFactory

if (sessionFactory == null ){

Configurationconf = new Configuration();

try {

Classklass = ClassforName( configHibernateCachableFileLoad );

IHibernateCachableFileLoadhibConf = (IHibernateCachableFileLoad)klassnewInstance();

hibConfaddMappings(conf);

} catch (ClassNotFoundExceptione){

// NOOP

} catch (InstantiationExceptione){

// NOOP

} catch (IllegalAccessExceptione){

// NOOP

}

Configurationconfdone = nfigure();

if (confdone != null ){

// Usedefaulthibernatecfgxml

sessionFactory = confdonebuildSessionFactory();

}

}

return sessionFactory;

}

}

config/HibernateCachableFileLoadjava

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

你需要修改如下部分

* 将你所有的Hibernate映射配置文件(*hbmxml)加入到程序中(正如你在Hibernatecfgxml中所做的)

package config;

import baudscatalinaIHibernateCachableFileLoad;

import orghibernatecfgConfiguration;

// Thisclassiswebappspecificandallowloadingofmappingvia

// addCachableFile();

public class HibernateCachableFileLoad implements IHibernateCachableFileLoad{

public void addMappings(Configurationconf){

doFile(conf com/mydomain/MyClassFilehbmxml );

doFile(conf com/mydomain/MyClassFilehbmxml );

}

private void doFile(ConfigurationconfStringresPath){

Stringpath = null ;

URLu = this getClass()getClassLoader()getResource(resPath);

if (u != null ){

path = ugetFile();

if (path != null )

conf = confaddCacheableFile(path);

}

if (path == null || conf == null )

Systemerrprintln( ERROR:Failedtoload: + resPath);

}

}

hibernatecfgxml

这将使我们标准的hibernatecfgxml发生一些变化如果你使用的是hibernateconfigurationdtd(版本)那么你可以不写入任何mapping元素

如果你使用的是老版本的dtd那么你需要在hibernatecfgxml中写入一个mapping元素

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

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

你需要作如下修改

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

那么现在

PUBLIC"-//Hibernate/HibernateConfigurationDTD//EN"

"-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<propertyname="connection.datasource">java:comp/env/jdbc/ConfigureMeDSproperty>

session-factory>

hibernate-configuration>

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

uk/mydomain/Dummy.hbm.xml

"-//Hibernate/HibernateMappingDTD3.0//EN"

"-mapping-3.0.dtd">

<hibernate-mapping>

<classname="uk.mydomain.Dummy"table="dummy">

<idname="id"type="long"column="id">

<generatorclass="native"/>

id>

class>

hibernate-mapping>

uk/mydomain/Dummy.java

packageuk.mydomain;

publicclassDummy{

privatelongid;

privatelonggetId(){

returnid;

}

privatevoidsetId(longid){

this.id=id;

}

}

上一篇:初学者对Hibernate的学习方法

下一篇:NetBeans 和 JBoss 结合使用的入门指南