I defined a second level cache in my application using @Cache annotation
I am using findById query, as the following:
long id = 4;
Company cmp = companyDAO.findById(id);
Where Company is the object that I get from the DB.
How can I check if the Company object came from the DB or from the cache?
Performance degrade. Yes, having caching do NOT necessary to give you better performance. Hibernate needs to do extra work to store and update the cache. If the entities cached are changed frequently and you are not querying them frequently, enabling the cache is just adding unnecessary extra burden.
Hibernate also implements a cache for query resultsets that integrates closely with the second-level cache. This is an optional feature and requires two additional physical cache regions that hold the cached query results and the timestamps when a table was last updated.
Hibernate second level cache uses a common cache for all the session object of a session factory. It is useful if you have multiple session objects from a session factory. SessionFactory holds the second level cache data. It is global for all the session objects and not enabled by default.
How can I check if the Company object came from the DB or from the cache?
Hibernate uses a specific category to Log all second-level cache activity. The relevant category is org.hibernate.cache
, just enable debug for it in the configuration of your logging framework.
See Chapter 3.5 Logging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With