Hibernate caching       2012     Barnabás Südybarnabas.sudy@gmail.com
Structure                                                          Three cache1st level cache - Session cache  Hibernate s...
Default behavior    Hibernate does not run queries (at all)    Query cache can store the    (Query, List<PrimaryKey>) pa...
Read phenomena    Phantom reads    Other transaction modifies the data    Non Repeatable reads    Other transaction adds...
Isolation levels      Serializable   --- Phantom reads ---   Repeatable read--- Non repeatable reads ---   Committed read ...
2nd level cacheTo optimise the performance Hibernate introduced the Cache strategies:    Read only    Read-Write    Non...
2nd level cacheTo optimise the performance Hibernate introduced the Cache strategies:    Read only    Read-Write    Non...
Read only strategyA read-only cache is good for data that  needs to be read often but not  modified.    Simple    Safe t...
NonStrict Read-WriteRarely need to modify data.This is the case if two transactions are unlikely to try to update the   sa...
Read-WriteAppropriate for an application which needs to update data regularly. (not exacly true)    Do not use a if seria...
How is works - Read-only    The modification is disabled.    No 2nd level cache update needed.
How is works -                    NonStrict Read-Write    The entities are evicted from the 2nd    level cache by their l...
How it works - Read-write    Transaction and entity timestamp.    If transaction timestamp is older than the    entity t...
How it works - Read-writeLets take a look our test code.
Good to know    Be careful with query cache without 2nd level    cache.    Be careful 2nd level cache without query    c...
A little bit configuration....    Persistence.xml    Annotations    Ehcache.xml    Etc.
Upcoming SlideShare
Loading in …5
×

Hibernate caching

1,617 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,617
On SlideShare
0
From Embeds
0
Number of Embeds
163
Actions
Shares
0
Downloads
69
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Hibernate caching

  1. 1. Hibernate caching 2012 Barnabás Südybarnabas.sudy@gmail.com
  2. 2. Structure Three cache1st level cache - Session cache Hibernate supported cache. Ensures the entities are returnd always on the same reference 1/session (entitymanager) - Not thread safeQuery cache Not supported by default. (Ehcache) Stores the queries and their results. 1/sessionfacory (entitymanagerfactory)2nd level cache Not supported by default (Ehcache) Stores the entities (dehydrated - not as objects) 1/sessionfactory (entitymanager)
  3. 3. Default behavior Hibernate does not run queries (at all) Query cache can store the (Query, List<PrimaryKey>) pairs. 2nd level cache stores the entities.Questions: What piece of data should be cached? Hibernate has to know which piece of data valid in the cache.
  4. 4. Read phenomena Phantom reads Other transaction modifies the data Non Repeatable reads Other transaction adds new data Dirty data Not committed data in the result.
  5. 5. Isolation levels Serializable --- Phantom reads --- Repeatable read--- Non repeatable reads --- Committed read --- Dirty data --- Uncommitted read
  6. 6. 2nd level cacheTo optimise the performance Hibernate introduced the Cache strategies: Read only Read-Write NonStrict Read-Write Transactional
  7. 7. 2nd level cacheTo optimise the performance Hibernate introduced the Cache strategies: Read only Read-Write NonStrict Read-Write Transactional
  8. 8. Read only strategyA read-only cache is good for data that needs to be read often but not modified. Simple Safe to use in a clustered environment. The data has to never ever modify. Best performance.
  9. 9. NonStrict Read-WriteRarely need to modify data.This is the case if two transactions are unlikely to try to update the same item simultaneously. No strict transaction isolation Ensure Session.close() or Session.disconnect() is called at the end of transaction. (except JTA) hibernate.transaction.manager_lookup_class to use in JTA environment.
  10. 10. Read-WriteAppropriate for an application which needs to update data regularly. (not exacly true) Do not use a if serializable isolation needed. Ensure Session.close() or Session.disconnect() is called at the end of transaction. (except JTA) hibernate.transaction.manager_lookup_class to use in JTA environment.
  11. 11. How is works - Read-only The modification is disabled. No 2nd level cache update needed.
  12. 12. How is works - NonStrict Read-Write The entities are evicted from the 2nd level cache by their life timeout. Until the eviction they are in use. Repeatable reads problem is not solved. The queries are not forwarded to the database.
  13. 13. How it works - Read-write Transaction and entity timestamp. If transaction timestamp is older than the entity timestamp the entity has to be pulled out from the database. The responsibility is deflected to the database.
  14. 14. How it works - Read-writeLets take a look our test code.
  15. 15. Good to know Be careful with query cache without 2nd level cache. Be careful 2nd level cache without query cache. Be careful query cache and read-write entity. And again: 2nd level cache is not queryable only findable. The first level cache keeps the entity references.
  16. 16. A little bit configuration.... Persistence.xml Annotations Ehcache.xml Etc.

×