Quantcast
Channel: Learning the code way
Viewing all articles
Browse latest Browse all 231

CacheModes in Hibernate - continued

$
0
0
In the previous post we saw the GET and IGNORE values for CacheMode. The other values are PUT and REFRESH. Consider the below code:
publicstatic void testPutMode() {
Statistics statistics = sessionFactory.getStatistics();

Session session1 = sessionFactory.openSession();
System.out.println("Cache Put Count : "
+ statistics.getSecondLevelCachePutCount());
session1.get(BookType.class, 1);
System.out.println("Cache Put Count : "
+ statistics.getSecondLevelCachePutCount());
session1.close();
System.out.println("Running in PUT mode");
Session session2 = sessionFactory.openSession();
session2.setCacheMode(CacheMode.PUT);
session2.get(BookType.class, 1);
session2.get(BookType.class, 2);
session2.get(BookType.class, 3);
System.out.println("Cache Put Count : "
+ statistics.getSecondLevelCachePutCount());
System.out.println("Cache Hit Count : "
+ statistics.getSecondLevelCacheHitCount());
session2.close();
}
In the PUT mode, the session will add records to the second level cache. However it will not read any records from the cache. As the second session uses the PUT CacheMode it does nor read the record with id 1 from the cache. It fires three select queries for all three records, adding two of them to the cache. The output from above method is :
Cache Put Count : 0
Hibernate:
/* load com.object.cache.BookType */ 
    select
booktype0_.ID as ID0_0_,
booktype0_.NAME as NAME0_0_
from
BOOK_TYPE_MASTER booktype0_
where
booktype0_.ID=?
Cache Put Count : 1
Running in PUT mode
Hibernate:
/* load com.object.cache.BookType */
select
booktype0_.ID as ID0_0_,
booktype0_.NAME as NAME0_0_
from
BOOK_TYPE_MASTER booktype0_
where
booktype0_.ID=?
Hibernate:
/* load com.object.cache.BookType */
select
booktype0_.ID as ID0_0_,
booktype0_.NAME as NAME0_0_
from
BOOK_TYPE_MASTER booktype0_
where
booktype0_.ID=?
Hibernate:
/* load com.object.cache.BookType */
select
booktype0_.ID as ID0_0_,
booktype0_.NAME as NAME0_0_
from
BOOK_TYPE_MASTER booktype0_
where
booktype0_.ID=?
Cache Put Count : 3
Cache Hit Count : 0

The other CacheMode is REFRESH. This has the same behavior as PUT (Or does it ?) From the Hibernate docs:
CacheMode.PUT: will write items to the second-level cache. 
Do not read from the second-level cache
CacheMode.REFRESH: will write items to the second-level cache.
Do not read from the second-level cache.
Bypass the effect of hibernate.cache.use_minimal_puts forcing
a refresh of the second-level cache for all items read from the database

I tested the above method for Refresh but did not find any difference in the result. I found one link that seemed to add something to this explanation.
What is the default value for CacheMode ?
publicstatic void testDefaultCacheMode() {
Session session = sessionFactory.openSession();
System.out.println(session.getCacheMode());
}
The output is :
NORMAL

Viewing all articles
Browse latest Browse all 231

Trending Articles