Prev Next

Hibernate / Hibernate Caching

1. Explain hibernate caching.

Caching optimizes the application performance and it lies between the application and the database to minimize the number of database calls as many as possible to provide better performance for performance critical applications.

Read full answer

2. What are the different caching techniques in hibernate?

Hibernate provides three type of cache. First level cache : First level cache is associated with session object in hibernate and hibernate uses it by default. Second level cache : Second level cache associates with session factory object. To avail second level cache, we need to configure it in ou...

Read full answer

3. Explain second level cache in hibernate.

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 also.

Read full answer

4. What are the Caching implementations in Hibernate?

Hibernate supports four open-source cache implementations. EHCache (Easy Hibernate Cache), OSCache (Open Symphony Cache), Swarm Cache, and JBoss Tree Cache. Each cache has different performance, memory use, and configuration possibilities.

Read full answer

5. Advantages of EHCache (Easy Hibernate cache).

The below are the advantages of EHCache. (org.hibernate.cache.EhCacheProvider) It is fast. lightweight. Easy-to-use. Supports read-only and read/write caching. Supports memory-based and disk-based caching. Does not support clustering.

Read full answer

6. Advantages of OSCache (Open Symphony Cache) in hibernate.

The below are the advantages of OSCache. (org.hibernate.cache.OSCacheProvider) It is powerful . flexible package. supports read-only and read/write caching. Supports memory- based and disk-based caching. Provides basic support for clustering via either JavaGroups or JMS.

Read full answer

7. Explain about SwarmCache in Hibernate.

SwarmCache (org.hibernate.cache.SwarmCacheProvider) is a cluster-based caching. supports read-only or nonstrict read/write caching. appropriate for applications those have more read operations than write operations.

Read full answer

8. Explain about JBoss TreeCache in Hibernate.

JBoss TreeCache (org.hibernate.cache.TreeCacheProvider) is a powerful replicated and transactional cache which is useful when we need a true transaction-capable caching architecture .

Read full answer

9. Explain shared cache mode in second level cache of Hibernate.

To configure shared cache mode, hibernate provides javax.persistence.sharedCache.mode and it allows four values. ENABLE_SELECTIVE : Entities will not be cached until entity will be annotated by cacheable. DISABLE_SELECTIVE : Those entities are cached which are explicitly not annotated with cachea...

Read full answer

10. What are the different CacheMode in second level cache of hibernate?

There are 4 different CacheMode. CacheMode.NORMAL : This mode allows to read and write data in second level cache. CacheMode.GET : In this mode, data will be read but will not be written in second level cache. CacheMode.PUT : Data will be written but not will be read from second level cache. Cach...

Read full answer

11. Explain query cache and It's configuration in hibernate.

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_...

Read full answer

12. Explain the different cache concurrency strategy of second level cache in hibernate.

In hibernate, cache concurrency strategy can be set globally using the property hibernate.cache. default_cache_concurrency_strategy. The allowed values are, read-only : supported by ConcurrentHashMap, EHCache, Infinispan. read-write : supported by ConcurrentHashMap, EHCache. nonstrict-read-write ...

Read full answer

13. How do I enable Second level Cache using hibernate annotation?

In hibernate first level cache is provided by default. Hibernate do that by session. But second level cache can be enabled explicitly. Second level Caching in Hibernate can be done at three levels. Enable Hibernate Second Level Cache Globally. We need to set a property in our persistence configur...

Read full answer

14. What does hibernate.cache.use_structured_entries perform in hibernate?

This entry forces Hibernate to store data in the second-level cache (L2 cache) in a more human-friendly format.

Read full answer

15. How second level cache works in Hibernate?

Whenever hibernate session try to load an entity, it first looks for cached copy of entity in first level cache (associated with particular hibernate session).If cached copy of entity is present in first level cache, it is returned as result of load method. If there is no cached entity in first l...

Read full answer

16. Can we disable first level cache in Hibernate?

Hibernate first level cache is enabled by default and there is no way to disable it. However hibernate provides methods using which selected objects can be deleted from the cache or clear the cache completely.

Read full answer

17. Explain evict method in Hibernate.

session.evict(obj) will remove the object instance from the session cache. Therefore if you are saving the object for the first time, you will have to explicitly commit via session.save(obj) before evicting the object from the cache. Subsequent update calls should follow through session.saveOrUpd...

Read full answer

18. Difference Between session.evict vs clear in Hibernate.

evict() evicts a single object from the session. clear() evicts all the objects in the session. Calling clear() is like calling evict() on every object associated with the session.

Read full answer

19. How do you find if an particular object exists at session cache?

We can use session contains() method to check if an object is present in the hibernate cache or not, if the object is found in cache, it returns true or else it returns false.

Read full answer

20. Role of eternal and maxElementsInMemory in ehcache.

eternal attribute maintains the data freshness and if true it indicates that mappings never expire. maxElementsInMemory is a count based capacity control that limits the cache size so that you do not exhaust memory of your application by filling up a cache.

Read full answer

21. How to disable hibernate second level cache?

Hibernate second level cache can be disabled by, setting use_second_level_cache as false , using CACHEMODE.IGNORE , Using cache provider as org.hibernate.cache.NoCacheProvider.

Read full answer

22. Difference between time to live and time to idle in Ehcache.

timeToIdleSeconds enables cached object to be invalidated if it has not been requested for specified ('timeToIdleSeconds' ) seconds. timeToLiveSeconds enables cached object to be invalidated after that many seconds regardless of how many times or when cache was requested last time. Note that the ...

Read full answer

23. Different Cache concurrency strategies in Hibernate.

read-only, nonstrict read-write, read-write, and transactional.

Read full answer

24. Explain collection cache in Hibernate.

Second Level Cache caches entities. Also, the second level cache also allows users to cache entity relationship information. Hibernate provides a collection cache , where it caches the primary keys of entities that are members of a collection field in another entity type. Say, for example, we hav...

Read full answer

25. Effect of native query on hibernate cache.

When using native SQL queries you have to address the risk of invalidating existing cache when query cache enabled. Using SQLQuery, Hibernate couldn't know what cache regions you might affect, however you can explicitly provide the instruction. With the explicit instruction, hibernate know which ...

Read full answer

«
»

Comments & Discussions