Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Temporary solution to improve hibernate startup
PostPosted: Wed Sep 21, 2005 3:29 pm 
Beginner
Beginner

Joined: Mon Feb 07, 2005 10:40 pm
Posts: 22
I have a Eclipse RCP app that uses hibernate. Everytime I start the App building the configuration and sessionFactory takes between 20 to 30 secs because of the parsing of xml files.
I have decided to use the addCacheableFile method that reads cacheable mapping files from an alternative hibernateCached.cfg.xml. and I have improved the startup from about 30 secs to 6 secs.

It would be really nice if hibernate includes an option for doing something like :

<mapping resource="com/estudiowebs/HibernateMappings/UsuarioRegistroVisitasSalidas.hbm.xml"
cached="true"
/>

until them here is my temporary solution for those who are interested on improving startup time. Feel free to reply here or write me at info@estudiowebs.com. The code is system dependent, make sure to review it and feel free to post any suggestions or change it to fit your needs

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

<hibernate-configuration>

<session-factory>
<property name="myeclipse.connection.profile"></property>
<property name="connection.url"></property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>

<!--
Mapping files to be cached have to be defined at hibernateCached.cfg.xml
If duplicates exist in both files will result to an exception.
To add a new file to cache it needs to be commented out in hibernateCached.cfg.xml and added here
Start up the sessionFactory and then removed from here and added to hibernateCached.cfg.xml
-->

</session-factory>

</hibernate-configuration>


------------------------------
hibernateCached.cfg.xml
------------------------------


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

<!-- Files Cached to improve performance -->

<hibernate-configuration>

<session-factory>

<mapping
resource="com/estudiowebs/yourfile.hbm.xml" />


</session-factory>

</hibernate-configuration>


------------------------------
HibernateSessionFactory.java
------------------------------

package com.estudiowebs.Hibernate;

import java.util.ArrayList;
import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import com.estudiowebs.Util.HibernateCacheableClassReader;


public class HibernateSessionFactory {


private static final String CONFIG_FILE_LOCATION = "/com/estudiowebs/hibernate/hibernate.cfg.xml";


/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();

/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();

/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;


public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null) {
if (sessionFactory == null) {
try {


ArrayList mappings = HibernateCacheableClassReader.getDeclaredMappings("C:\\Documents and Settings\\Administrator\\workspace\\com.estudiowebs.RafaCentroRCP\\src\\com\\estudiowebs\\Hibernate\\hibernateCached.cfg.xml");
Iterator i = mappings.iterator();

while (i.hasNext()) {
cfg.addCacheableFile((String)i.next());
}

cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
// sessionFactory = cfg.addCacheableFile(CONFIG_FILE_LOCATION).configure().buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}

return session;
}

public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

private HibernateSessionFactory() {
}

public static String getCONFIG_FILE_LOCATION() {
return CONFIG_FILE_LOCATION;
}

}


------------------------------
HibernateCacheableClassReader .java
------------------------------

package com.estudiowebs.Util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class HibernateCacheableClassReader {


public static ArrayList getDeclaredMappings(String configFilePath) throws DocumentException {

String configFileFolder = configFilePath.replaceAll("hibernateCached.cfg.xml","").replaceAll("Hibernate","HibernateMappings");
File configFile = new File(configFilePath).getAbsoluteFile();

ArrayList mappings = new ArrayList();

SAXReader reader = new SAXReader();
org.dom4j.Document doc = reader.read(configFile);
XPath xpathSelector = DocumentHelper.createXPath("/hibernate-configuration/session-factory/mapping");
List results = xpathSelector.selectNodes(doc);
Iterator i = results.iterator();
while (i.hasNext()) {
Element element = (Element) i.next();
String fileName = configFileFolder + element.attribute("resource").getText().replaceAll("com/estudiowebs/HibernateMappings/","");
mappings.add(fileName);
}

return mappings;

}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 3:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
better location for this is on our wiki and add the request for cfg.xml enhancement to jira (otherwise it will be forgotten)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:21 pm 
Beginner
Beginner

Joined: Mon Feb 07, 2005 10:40 pm
Posts: 22
what is jira, and where can I find it?

Thanks .

Raul.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
http://hibernate.org/217.html

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 9:17 am 
Beginner
Beginner

Joined: Thu Aug 11, 2005 5:19 am
Posts: 29
Don't works for me!
Seems to be a problem with struts-menu plugin using Hibernate DAO...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.