Home  >  Hibernate Interview Q & A

Hibernate Interview Questions: Cache in Hibernate

December 13, 2013
Qns-1: How many types of cache do hibernate provide?
Ans: Hibernate provides three type of cache.
a. First level cache : First level cache is associated with session object in hibernate and hibernate provides it by default.
b. Second level cache : Second level cache associates with session factory object. To avail second level cache, we need to configure it in our application.
Qns-2: What is second level cache in hibernate?
Ans: Second level cache is on SessionFactory level. All the second level cache provider class must implement org.hibernate.cache.spi.CacheProvider by configuring the property hibernate.cache.provider_class. Hibernate provides open source cache providers aslo. Find the examples of second level cache.

a. ConcurrentHashMap (for testing purpose)
b. EHCache
c. Infinispan
Qns-3: What is the shared cache mode in second level cache?
Ans: To configure shared cache mode, hibernate provides javax.persistence.sharedCache.mode and it has three values.
a. ENABLE_SELECTIVE : Entities will not be cached until entity will be annotated by cacheable.
b. DISABLE_SELECTIVE : Those entities are cached which are explicitly not annotated with cacheable.
c. ALL : Every entities will be cached.
d. NONE: No entity will be cached.
Qns-4: What are the cache concurrency strategy values for the property?
Ans: In hibernate, cache concurrency strategy can be set globally using the property hibernate.cache.default_cache_concurrency_strategy. The values which can be assigned are:

a. read-only : supported by ConcurrentHashMap, EHCache, Infinispan.
b. read-write : supported by ConcurrentHashMap, EHCache.
c. nonstrict-read-write : supported by ConcurrentHashMap, EHCache.
d. Transactional : supported by EHCache, Infinispan.

Find the example.
@Entity 
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Student{ }
Qns-5: What the values of CacheMode in second level cache are in hibernate?
Ans: There are four values of CacheMode.
a. CacheMode.NORMAL : This mode allows to read and write data in second level cache.
b. CacheMode.GET : In this mode, data will be read but will not be written in second level cache.
c. CacheMode.PUT : Data will be written but not will be read from second level cache.
d. CacheMode.REFRESH : Data will be written and will not be read from second level cache. The difference between Put and Refresh is that CacheMode.REFRESH will bypass hibernate.cache.use_minimal_puts effect.
Qns-6: What is query cache and how to configure it?
Ans: Hibernate can cache query results. When a query is fetched frequently then caching query result is useful. Hibernate has an overhead to enable query cache because to keep updated the query result, hibernate has to track the changes in database. We configure it as below.
hibernate.cache.use_query_cache = "true"
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us