在tomcat5.5.x环境下,调用configuration().addcacheablefile来载入配置,建立hibernate sessionfactory,成功地提高了载入速度。
推荐你只是在开发阶段采用这样的方式载入,最后的产品发布阶段你仍需使用经典的hibernate.cfg.xml文件,通过tomcat的servletcontextlistener api在应用程序部署的时候建立hibernate sessionfactory,而不是在程序第一次调用hiberante的时候。
文件:
net/netbauds/catalina/ihibernatecachablefileload.java
这个文件可以在不同的web应用中使用而不用作任何修改。
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)。
这个文件也可以在不同的应用中使用而不加任何修改:
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中所做的)。
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 修改为你自己的数据库连接信息
那么现在:
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< span>property>
mapping files -->
< span>session-factory>
< span>hibernate-configuration>
如果你使用的hibernate版本需要在hibernate.cfg.xml中至少有一个mapping元素,那么你可能需要用到下面的两个文件,否则,如上就可以了。
uk/mydomain/dummy.hbm.xml
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" />
< span>id>
< span>class>
< span>hibernate-mapping>
uk/mydomain/dummy.java
public class dummy {
private long id;
private long getid() {
return id;
}
private void setid(long id) {
this.id = id;
}
}
闽公网安备 35060202000074号