manoj s
manoj s

Reputation: 281

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

Upvotes: 6

Views: 1477

Answers (2)

Daya
Daya

Reputation: 71

  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.

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691715

  1. That's a too broad question to be answered here.

  2. No. It populates the cache lazily. Each time you get a cachable entity from the database, using the hibernate API or a query, this entity is stored in the cache. Later, when session.get() is called with an ID of an entity that is in the cache, no database query is necessary.

  3. If the update is made through Hibernate, then the cache is updated. If it's done using an external application, or a SQL query, or even a bulk update HQL query, then the cache is unaware of the update. That's why you need to be careful about which entities you make cachable, which time-to-live you choose, etc. Sometimes, returning stale values is not problematic, and sometimes it is unacceptable.

  4. No.

Upvotes: 0

Related Questions