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;
}
}