A consultant company is developing a new application with Hibernate, Spring Framework and OSCache for us. The selection of these tools had been made by different reasons. We have some performance issues and investigated them.
Our logs are showing a lot of cache misses like
Quote:
com.mycompany.app.document.DocumentTypeInternationalPK@10cfd8[documentType=com.mycompany.app.document.DocumentTypeBO@358d27[docTypeId=18],language=com.mycompany.app.document.LanguageBO@de77cd[lanId=EN]].com.mycompany.app.document.DocumentTypeInternationalBO
The reason of the cache misses are the object ids: @10cfd8, @358d27, ...
Each time Hibernate checks if an object is in the cache, it will generate a new object with a different object id. Hence hibernate / OSCache is not possible to find the object in the cache, because OSCache must have well-behaved toString methods.
The objects ids are created by the toString method in the hbm2java generated java source files (BasicRenderer). Example:
Quote:
public String toString() {
return new ToStringBuilder(this)
.append("comId", getComId())
.append("keyword", getKeyword())
.toString();
}
hbm2java uses the Apache Commons ToStringBuilder to create the return strings of the toString method. The class ToStringBuilder uses the ToStringStyle.DEFAULT_STYLE by default. In the DEFAULT_STYLE the attribute useIdentityHashCode is set to true, hence the object ids are containing f.i. "@10cfd8". It is necessary to create a hibernate to string style:
Quote:
public static final HIBERNATE_TO_STRING_STYLE = new StandardToStringStyle() {
public StandardToStringStyle() {
super();
setUseIdentityHashCode(false);
}
};
Changing the line
Quote:
return new ToStringBuilder(this)
to
Quote:
return new ToStringBuilder(this, HIBERNATE_TO_STRING_STYLE)
should fix the problem.
I don't think this is a problem of OSCache, because it's behaviour is by design. OK, other cache implementation uses the hashcode and the equals methods to differentiate between different cache objects. I think the problem should be fixed in the code generation of hbm2java, because it it the easiest way to fix it and some other caches may ran into the same problem.
Who ran into the same problem also? / Can somebody confirm this problem?
Should I contact the developers of hbm2java to fix the problem in BasicRenderer?
Should I raise an issue in JIRA?
http://www.opensymphony.com/oscache/hibernate.html see "NOTE: object identifiers must have well-behaved toString() methods."
Hibernate version: 2.1
Hibernate extensions: 2.1.2[/quote]