Hi all,
I am using hibernate 4.3.5 as JPA 2.1 provider.
I have a following problem: I want to use 2nd level cache but I want hibernate to reuse the data in the cache as much as possible. The reason for this is that I want to fetch large amounts of data from DB in the form of entities that are kept in memory longer than the hibernate session lasts (and I don't want to use extended persistence contexts). It would be very useful if I the data from the cache got reused instead of allocating new objects. For example if data for entity user(id:5) exists in the 2nd level cache, it would be desireable for query that returns corresponding row from the database to return the user object that has the same string object for user name that currently resides in the second level cache so that garbage collector can quickly remove the data from result set and free up memory.
I understand that this may not always be possible if we want to keep the cache updated, but I have only one application that uses the DB so there's no harm done. So far, I've investigated when exactly does hibernate reuse data from 2nd level cache, and the behavoiur seems quite erratic. First, it naturally reuses the data when I call EntityManager.find(), and that's fine. But when I run queries things get really complicated. The behavoiur seems to vary greatly depending on whether I use join fetch or not, depending on the type of fetch and so on.
So does anyone know the exact rules/configurations that govern the way hibernate reuses (or doesn't reuse) the data from 2nd level cache when fetching query results? Also is there a configuration that can help me tune this behavoiur to my needs? Also let me point out that using query cache is also out of the question because of it's tendency to pile up large amounts of data and to get invalidated quiclky.
|