From: 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 ;)
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/
|
posted on 2006-10-30 17:22
R.Zeus 阅读(850)
评论(0) 编辑 收藏 所属分类:
Hibernate 、
J2SE