We're experiencing a very strange problem in our web application. First, here are the relevant packages we are using:
spring-1.1.4
hibernate-2.1.8
ehcache-1.1
We have several hibernate-enabled domain objects that have a "displayOrder" property, which is used in conjunction with hibernate's <list> collection feature. We have used the override-the-getter trick in the domain object, e.g.:
Code:
public Integer getDisplayOrder() {
try {
return new Integer(getParent().getChildren().indexOf(this));
} catch (Exception e) {
return super.getDisplayOrder();
}
}
as mentioned at
http://www.hibernate.org/193.html (see the "Does it really work? note at the bottom of that page).
We are using ehcache as the second-level cache, no disk spillover, and the ttl for everything is 1 hour. Both entities and collections are being cached.
Here's the weird problem we are encountering:
Occasionally, <list> collections get corrupted. For example, parent.getChildren() might have 10 elements. But occasionally, parent.getChildren() might contain 100+ elements, only 10 of which are non-null. Looking in the database, the displayOrder column no longer goes from 0..9, it goes 0, 1, 15, 42, 76, ..., 100+.
Has anyone seen anything like this before? We have all sorts of logging in the application, but we have yet to be able to pinpoint the source of this corruption, nor have we been able to reproduce it!
We're wondering if there might be some concurrency issues going on, and I have some questions about that.
None of our manager or domain object methods are synchronized. We are using spring's OpenSessionInViewFilter, so each request handler thread has its own session and first-level cache. Now that we have enabled the second-level cache, we are wondering if we need to go through and mark many methods "synchronized".
I need to look through the Hibernate code, but I am assuming that when the second-level cache is enabled, the parent.getChildren() collection returned by Hibernate could reference the same collection in two separate threads. Is that true? Or, does hibernate clone() things out of the second-level cache?
Thanks for your help,
Peter
Code: