Hibernate caching
-
Upload
alex-verdyan -
Category
Technology
-
view
1.805 -
download
0
Transcript of Hibernate caching
![Page 1: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/1.jpg)
Hibernate Cache
Alex Verdyan
![Page 2: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/2.jpg)
Why cache?
Why it works?● Temporal locality● Non uniform distribution of hits
![Page 3: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/3.jpg)
Temporal locality
![Page 4: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/4.jpg)
Non-uniform distribution
![Page 5: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/5.jpg)
17,000 pageviews avg load = 250ms
cache 17 pages / 80% of viewscached page load time = 10 msnew avg load = 58ms
● Trade memory for latency reduction
● Reduce database load
![Page 6: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/6.jpg)
Hibernate cache
● Session level cache - L1● SessionFactory (JVM) cache - L2● Query Cache
![Page 7: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/7.jpg)
Session cache - L1
● Enabled by default● Used transparently during the session● All objects that was saved or retrieved
○ save ○ update○ get ○ list○ iterate
● flush() - will sync cache to DB● clear() - will evict all objects
![Page 8: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/8.jpg)
L2 cache - configuration
1. Get ehcache.jar and add it to classpath2.
3. Edit persistence.xml
![Page 9: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/9.jpg)
L2 Cache
● Inter-session cache ● Can be clustered● Configurable with XML or Annotations● Divided to regions
○ Entity - data○ Collections - relations○ Queries - query results ○ Timestamps - last update timestamp for tables
![Page 10: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/10.jpg)
L2 cache strategies
● Read-only○ simplest, safest and fastest○ supports insert, no update/delete○
● Nonstrict Read-Write○ occasional writes, no locking cache○ low prob. that 2 transactions will write the same
object○ async - update
![Page 11: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/11.jpg)
L2 cache strategies
● Read-Write○ read committed - transaction isolation○ uses soft locks ○ async - cache update out of tx
● Transactional○ only with JTA (i.e. with distributed tx support)○ serialized transaction isolation○ sync - cache update inside tx
![Page 12: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/12.jpg)
L2 cache - how it works
● Does not cache actual objects● Caches values of the properties● Mappings (relations) are not cached by
default○ it can helps with N+1 queries
![Page 13: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/13.jpg)
Example
● cache of the mapping is optional● although it's the best place to improve● beware of code altering the associations
and not cascading the change
![Page 14: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/14.jpg)
How will the cache look like*-----------------------------------------------------*
| Person Data Cache |
|-----------------------------------------------------|
| 1 -> [ "John" , "Bon Jovi" , null , [ 2 , 3 ] ] |
| 2 -> [ "Joey" , "Ramone" , 1 , [] ] |
| 3 -> [ "Sara" , "Connor" , 1 , [] ] |
*-----------------------------------------------------*
Hibernate cache stores "dehydrated" objects
![Page 15: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/15.jpg)
Evicting from cache
From L1 cache (Session)
session.evict(entity)
session.evict(Person.class)
From L2 cache (SessionFactory)
sf.getCache().evictEntity(Person.class,2L); sf.getCache().
evictCollection("com.bla.Person.children",1L)
![Page 16: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/16.jpg)
Query cache
![Page 17: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/17.jpg)
Query cache
What happens if don't query by Id ?
The query cache will look like:*---------------------------------------------------------
-*
| Query Cache
|
|---------------------------------------------------------
-|
| ["from Person as p |
| where p.parent.id=? |
| and p.firstName=?", [1, "Joey"]] -> [2]] |
*---------------------------------------------------------
-*
![Page 18: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/18.jpg)
Query cache
There are 3 cache regions:● StandardQueryCache
○ cached query results
● UpdateTimestampsCache○ holding timestamps of the most recent updates
● NamedQueryCaches ○ Query.setCacheRegion(name)
![Page 19: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/19.jpg)
Query Cache
QC is useful when you query by "Natural id"Criteria crit = session.createCriteria(Person.class);crit.add(
Restrictions.naturalId().set("email", "[email protected]")).
setCacheable(true). uniqueResult();
Since Natural Id immutable, Natural Id -> Primary Key mapping cannot be invalidatedThis allows Hibernate to bypass UpdateTimestampCache
![Page 20: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/20.jpg)
Query Cache - Pitfalls
● Any update to the underlying table updates the timestamp cache - invalidates all entities
● Memory ○ the queries are large pretty strings and the
bind variables can be objects
● Lock contention - QC has a coarse lock on timestamp cache for insert/update/delete and lookups
![Page 21: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/21.jpg)
Stuff to remember
● Cache cannot know about updates made to the persistent store by another application
● Query cache - unless you use a natural key is almost always a bad idea○ unless you know what you're turning it on○ you can show an improvement with realistic load
● TTL and TTI ○ tune them
![Page 22: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/22.jpg)
Cache providers
![Page 23: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/23.jpg)
Bundled cache providers Cache Type Cluster Safe Query Cache Supported
ConcurrentHashMap memory no yes
EHCache memory, disk,
transactional, clustered
yes yes
Infinispan (JBoss Cache) clustered (ip multicast),
transactional
yes yes (clock sync req.)
![Page 24: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/24.jpg)
Bundled cache providers Cache read-only nonstrict-read-write read-write transactional
ConcurrentHashMap yes yes yes
EHCache yes yes yes yes
Infinispan (JBoss cache) yes yes
![Page 25: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/25.jpg)
More cache providers
● Hazelcast● GemFire● Oracle Coherence● Gigaspaces
![Page 26: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/26.jpg)
Monitoring
![Page 27: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/27.jpg)
Questions
![Page 28: Hibernate caching](https://reader034.fdocuments.us/reader034/viewer/2022050817/555c25aed8b42a0b418b4c9c/html5/thumbnails/28.jpg)
References
http://tech.puredanger.com/2009/07/10/hibernate-query-cache/http://anirbanchowdhury.wordpress.com/2012/07/23/hibernate-second-level-cache-ehcache/#introductionhttp://www.javalobby.org/java/forums/t48846.htmlhttp://www.slideshare.net/alexmiller/cold-hard-cache