0 votes
in JAVA by
What are the different types of caches available in Hibernate?

1 Answer

0 votes
by
Caching is a mechanism to enhance the performance of a system. It is a buffer memory that 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.

Hibernate provides 3 types of caching.

Session Cache - The session cache caches objects within the current session. It is enabled by default in Hibernate. Objects in the session cache reside in the same memory location.

Second Level Cache - The second level cache is responsible for caching objects across sessions. When this is turned on, objects will first be searched in the cache and if they are not found, a database query will be fired. The second-level cache will be used when the objects are loaded using their primary key. This includes fetching of associations. Second-level cache objects are constructed and reside in different memory locations. The second-level cache is required to be configured with external cache provider like EhCache.

Query Cache - Query Cache is used to cache the results of a query. When the query cache is turned on, the results of the query are stored against the combination query and parameters. Every time the query is fired the cache manager checks for the combination of parameters and query. If the results are found in the cache, they are returned, otherwise, a database transaction is initiated.

Related questions

0 votes
asked Apr 14, 2023 in JAVA by SakshiSharma
0 votes
asked Apr 14, 2023 in JAVA by SakshiSharma
...