Hibernate version:2.1.6
Mapping documents: (Unless there really is a problem with my setup, this isn't really relevant.)
Code between sessionFactory.openSession() and session.close():(Unless there really is a problem with my setup, this isn't really relevant either.)
Name and version of the database you are using: MySQL (prolly not relevant)
After playing around with hibernate it appears that if you call Query.iterator() ( instead of Query.list() ) and Query.setCacheable(true), then hibernate will not perform query caching as requested. If this is expected, would it be possible to change hibernate to allow the query to be cached if Query.iterator() is called AND the end of the result set is reached? (P.S. In the mean time, I recommend adding to "14.5 The Query Cache" clearly stating that you must use Query.list() instead of Query.iterator(). It took me a little while to figure out that that was the problem. I kept thinking it was a problem with how I had ehcache configured.)
Does query caching only provide caching within a session? Meaning that the following pseudo code does not work.
for(int i=0; i<1000; i++) {
Session session = sessionFactory.openSession();
String queryStr = "from Account";
Query query = session.createQuery(queryStr);
query.setCacheable(true);
query.list();
s.closeSession();
}
From the tests that I have created the stuff above doesn't seem to work. If that's not the expected behavior, then I'll keep trying to make it work.
From a theoretical stand point, it seems like there shouldn't be too much difference between knowing whether a query has been invalidated because of data changes within a session versus more globally. e.g. in the above example, as long as no accounts were added or deleted (save() or delete()), we shouldn't have to go to the database again. Or more generally as long as no objects associated with the query has had a save(), update(), or delete() called on them, it should be safe to continue caching the query as long as no outside application changed data affecting the results of the query. By "association" I mean that a object is associated with a query by class type when that same class is referenced in the "from" or "where" clause or is in a subselect of the query.
It also seems like it would be appropriate to not automatically invalidate queries between sessions if the data is solely being pulled from objects who's mapping file contains "<cache usage="read-only"/>" and the criteria also stays within "read-only" objects/tables.
|