Performance: Hibernate startup timeThe following tips hints how to make Hibernate startup faster.
Only add the mapping files you need!
If you are running some few JUnit tests for a 400++ classes project you probably don't hit every class in those tests and thus do not need to add all those hbm.xml's to the Configuration. Go look at Hibernate's test suite on how you could let your TestCase decide what classes should be defined in the mapping.
Use serialized XML documents when configuring Configuration
When building the configuration 40-60% of the time is used by the XML parsers and Dom4j to read up the XML document. Significant performance increases can be done by serializing the Document object's to disc once, and afterwards just add them to the configuration by deserializing them first. In the current cvs we have an experimental Configuration.addLazyFile() method that can be used as inspiration for previous Hibernate versions.
public Configuration addLazyFile(String xmlFile) throws MappingException {
try {
File file = new File(xmlFile);
File lazyfile = new File(xmlFile + ".bin");
org.dom4j.Document doc = null;
List errors = new ArrayList();
if(file.exists() && lazyfile.exists() && file.lastModified()<lazyfile.lastModified()) {
log.info("Mapping lazy file: " + lazyfile.getPath());
ObjectInputStream oip = null;
oip = new ObjectInputStream(new FileInputStream(lazyfile));
doc = (org.dom4j.Document) oip.readObject();
oip.close();
} else {
doc = xmlHelper.createSAXReader(xmlFile, errors, entityResolver).read( file );
log.info("Writing lazy file to " + lazyfile);
ObjectOutputStream oup = new ObjectOutputStream(new FileOutputStream(lazyfile));
oup.writeObject(doc);
oup.flush();
oup.close();
}
if ( errors.size()!=0 ) throw new MappingException( "invalid mapping", (Throwable) errors.get(0) );
add(doc);
return this;
}
catch (Exception e) {
log.error("Could not configure datastore from file: " + xmlFile, e);
throw new MappingException(e);
}
}
Disable Hibernates usage of cglib reflection optimizer
Put the following line in hibernate.properties:
hibernate.cglib.use_reflection_optimizer=false
It will make Hibernate start faster since it does not try to build cglib-enhanced objects to access getter/setters. Note: It will have in impact on overall runtime performance since Hibernate will be forced to use standard JDK reflection for access. So it is most useful during development. (You will also get better error messages in some situations when the optimizer is disabled ;)
|