Issue
I have questions on the statements I found while going through hibernate caching tutorial.
Statement1:-First-level cache always Associates with the Session object. Hibernate uses this cache by default.
Question1:-
As this cache is by default holding all the results from session methods like load,get etc. Wont it be a major memory issue because of holding all the objects in seesion ?
Question2:-
Do we need to enable this cache or its there by default? Can I disable First level cache, if I want? If yes how?
Below questions are for both first level and second level cache
Question3:-
If I get the same object from cache(say I load the object customer with customerid 1 twice in the same session)both objects returned from cache will be having different references from the object lying in cache. Right? Because if I update some field in customer object, it should not reflect in cache until and unless save/update method is called.
Question4:-
As per my understanding if we try to get the customer object from id 1 second time from this cache, it will return the same old object even if it got updated in between. How can we ensure if it got updated it reads from database otherwise from cache? I guess we can use read write cache strategy with class like EHCache
Question5:- On Query Caching
I read this statement :-Updates in the queries occur very often. So, for query caching, two cache regions are necessary.
For storing the results.( cache identifier values and results of value type only). For storing the most recent updates.
I think “Updates in the queries occur very often” mean here we usually change some parameter value like select * from customer where custid=?
So id
will be changed for different customer id. So query cache will keep each query (along with parameter ) and results returned by each query.
Right? But not sure why do we need two cache region for query caching?
Solution
AFAIK
Answer 01:
Hibernate caches the objects loaded so as to avoid unwanted access to database. The simplest way to avoid a persistance context would be to use a StatelessSession
. A Stateless Hibernate Session has no dirty checking responsibility and hence goes well with your requirement of not having the first level cache(Persistant Context).
Answer 02:
- You don't have to enable first level cache
Answer 03
- If an object is loaded twice in the same session using its identity, Hibernate makes sure that the same object is returned regardless of whether the second level cache is enabled. Let me clarify that second level cache DOES NOT cache instances of object. It maintains a serialized (not exactly) version of the instance. The following link gives a brief explanation of structure of hibernate second level cache.
I am sorry but I fail to understand the concept behind "updating customer object and it not being reflected in the cache". I hope the link clarifies your question.
Answer 04
- Problems of stale data fetched from cache do arise if not configured correctly.
State data will be fetched if the expiration time on the cache has not occurred. Eviction can be carried out using
Session.evict()
however chances are you might not have a clear idea of what data needs to evicted. Hence caching should always be the final step in optimizing your access to database. Good level of optimization can be achieved by re-evaluating the associations and fetch-plans on collection relations so as to avoid N+1 selection issues and at the same time avoid.
EHCache providesread-write
caching stratergies for data which does not get updated that conforms to transactionread committed
protection. That is it avoid dirty reads but cannot help against repeatable reads.
Check out EHCache configuration here
Answer 05 : Query Caching
Check Out these links for better understanding of Session Cache and Query Cache
A. Session Cache
B. Second Level Cache
C. Query CacheCheckout this link which gives a brief/simpler explanation of structure of query cache.
Hope this helps.
Answered By - frictionlesspulley
Answer Checked By - Marilyn (JavaFixing Volunteer)