Member Menu
 
 Monthly JBoss newsletter:
 
Hibernate Books
CaveatEmptor

Performance: Hibernate startup time

The 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.addCachableFile() method that can be used as inspiration for previous Hibernate versions.

public Configuration addCachableFile(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 ;)


  NEW COMMENT

Serializing the Configuration object 04 May 2004, 12:37 luish
Another approach would be to serialize the whole Configuration 
object. What do you think about this? I have submitted a patch to  
the Jira to make the Configuration Serializable (see bugs 492 and 
147).
 
addLazyFile() not there. 06 Jul 2004, 11:50 gstamp
I can't fine addLazyFile() in CVS.  Is it still supposed to be there?
 
Hibernate3 feature 31 Aug 2004, 11:13 gavin
Try the Hibernate3 module (or just the alpha release)
 
Information update? 30 Mar 2005, 07:54 gruberc
The information on this page does not seem to be correct any
more. With Hibernate 3.0rc1, there is no Configuration.addLazyFile()
any more, but addCacheableFile(). How should it be used?
 
lazy 06 Mar 2006, 05:09 steckemetz
If you have terrible problems with startup time and
do NOT need certain features like:

* proxy objects
* lazy loading

or if you are using the stateless session,
then you can disable lazyness on class level like:

<class name="myClass" table="myTable" lazy="false">

The default is true and forces byte code generation of
some proxy class which takes a lot of time.
Perhaps some hibernate guru can tell us, which other
features will be disabled by this.
 
I solve it. 02 Aug 2006, 00:12 cm4ever
The Hibernate Configuration module implement is very bad.
I write a module to realize the dynamic loading mapping files.
But other function I can't resolve...

Hibernate Dynamic Module
This project is only a module of Hibernate http://www.hibernate.org Read
mapping file until insert/update/delete/select the persistent class in
Hibernate.
http://sourceforge.net/projects/hbn-dyn-mod/
 
Profiler output, no difference for CGLIB on or off ??? 11 Dec 2006, 23:10 gschadow
I tried cglib_optimize = false and still it takes long. Here is profiler
output:

hibernate.bytecode.use_reflection_optimizer = false:

                     total time times 
buildSessionFactory:   15.128 s     1 for 58 classes
  createClassPersister: 8.291 s    58 
  postInstantiate:      3.669 s    58 
  checkNamedQueries:    2.470 s     1 for 25 queries 

hibernate.bytecode.use_reflection_optimizer = true:

                     total time times
buildSessionFactory:   15.334 s     1 for 58 classes
  createClassPersister: 8.343 s    58 
  postInstantiate:      3.819 s    58
  checkNamedQueries:    2.437 s     1 for 25 queries 

Seems like there is no difference! Strange. It's excruciating time. I
think the only way out of this is to use dynamic class re-loading and
keep the JVM up rather than restarting the JVM. That way, the
SessionFactory can survive edit-and-test cycles. 

Really this whole thing is mostly an issue of testing. For the end user
app it is not such a big deal to have a 20 s delay on startup, as long
as your model is to keep the app up for a long time. Word or even
Acrobat reader takes quite some time to start up too.
 
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]