Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Needs clarity on hibernate second level cache

I need some clarification with the Hibernate second level cache.

  1. How does the hibernate second level cache works?

  2. Does it loads all the data from the tables for which there is @Cacheable annotation (with respect to hibernate annotation) in the entity classes on server start up in the Java EE environment?

  3. Will the cache gets sync up when there is an update on those tables and how?

  4. Last one is there any ways for my DAO code to get notified when there is an updated on some table which i am interested upon? (Looking for any listener which can intimate abt the updates of the tables).

like image 702
manoj s Avatar asked Oct 22 '12 17:10

manoj s


People also ask

Which 2nd level cache is better in Hibernate?

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.

What is the disadvantage of 2nd level caching?

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.

Why do we need second level cache in Hibernate?

Why Is a Second-Level Cache Important for Hibernate? A second-level cache improves application performance with regard to persistence for all sessions created with the same session factory.


1 Answers

  1. How does the hibernate second level cache works?

    When your entity is marked as cacheable and if you have configured the second level cache then hibernate will cache the entity to the second level cache after the first read.

    Hibernate provides the flexibility to plugin any cache implementation that follows hibernates specification. Refer Hibernate Manual for more details on second level cache and configurations options.

  2. Does it loads all the data from the tables for which there is @Cacheable annotation (with respect to hibernate annotation) in the entity classes on server start up in the Java EE environment?

    I don't think there is any configuration for achieving this. Indirectly you can achieve this by reading the entire table in startup, this can adversely affect the system startup time. (i don't prefer this). If the entity is modified externally, then hibernate can't sync it and you will end up getting stale data.

  3. Will the cache gets sync up when there is an update on those tables and how?

    The cache won't get updated instantly after the table update. The subsequent call to fetch the updated record will go the database, hibernate achieves this internally by using session timestamps.

  4. Last one is there any ways for my DAO code to get notified when there is an updated on some table which i am interested upon? (Looking for any listener which can intimate abt the updates of the tables).

    No, hibernate doesn't support this.

like image 56
Daya Avatar answered Oct 13 '22 21:10

Daya