Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where and how to check that hibernate cache really works

I am new in the hibernate's cache area.

  1. What is the easiest way to check that the cache really works?
  2. Does hibernate gnerate the same sql statements when cache is on?
  3. Should it be any folder/file in filesystem with stored data (second-level cache)?
  4. How to check how much cache is currently used?

Regards, Marcin

like image 838
Marcin Sanecki Avatar asked Nov 03 '11 13:11

Marcin Sanecki


People also ask

Where does Hibernate store cache?

Hibernate Session is it's first level cache. It is object in heap, so it is in RAM.

Which cache is best for Hibernate?

Hibernate 2nd level cache is best suitable for data that rarely or never changes. However since hibernate provides a generalized caching provider implemented by multiple caching solutions, it limits usability of features provided by caching solutions.

How does Hibernate cache work?

Hibernate in Practice - The Complete Course Caching is a mechanism to enhance the performance of a system. It is a buffer memorythat lies between the application and the database. Cache memory stores recently used data items in order to reduce the number of database hits as much as possible.

Which cache service is default for Hibernate?

Hibernate First Level cache is enabled by default, there are no configurations needed for this. Hibernate first level cache is session specific, that's why when we are getting the same data in same session there is no query fired whereas in other session query is fired to load the data.


2 Answers

  1. You can enable Hibernate statistics generation be setting hibernate.generate_statistics property to true. Then you can monitor cache hit/miss count via SessionFactory.getStatistics().

    Also, when SQL logging is enabled you can analyze cache behaviour by presence or absense of particular SQL queries.

  2. It depends on many factors. See 21.2. The Second Level Cache and 21.4. The Query Cache

  3. It depends on cache provider and its configuration. For example, EhCache can be configured to overflow to disk.

  4. SessionFactory.getStatistics().getSecondLevelCacheStatistics() provides this information.

like image 65
axtavt Avatar answered Sep 23 '22 06:09

axtavt


I have found nice tool that vizuliaze hibernate usage (also cache). Additionaly in these articles (here is part one) there is quite good explanation how the hibernate cache works (see also related links). There is also nice example with analysis steps.

Anyway thanks for answers.

like image 29
Marcin Sanecki Avatar answered Sep 20 '22 06:09

Marcin Sanecki