use of javax.persistence.Cache in project microservices by pwillhan.
the class SecondLevel method cacheControl.
@Test
public void cacheControl() throws Exception {
CacheTestData testData = storeTestData();
Long USER_ID = testData.users.getFirstId();
Long ITEM_ID = testData.items.getFirstId();
EntityManagerFactory emf = JPA.getEntityManagerFactory();
Cache cache = emf.getCache();
assertTrue(cache.contains(Item.class, ITEM_ID));
cache.evict(Item.class, ITEM_ID);
cache.evict(Item.class);
cache.evictAll();
org.hibernate.Cache hibernateCache = cache.unwrap(org.hibernate.Cache.class);
assertFalse(hibernateCache.containsEntity(Item.class, ITEM_ID));
hibernateCache.evictEntityRegions();
hibernateCache.evictCollectionRegions();
hibernateCache.evictNaturalIdRegions();
hibernateCache.evictQueryRegions();
}
use of javax.persistence.Cache in project microservices by pwillhan.
the class SecondLevel method cacheNaturalId.
@Test
public void cacheNaturalId() throws Exception {
CacheTestData testData = storeTestData();
Long USER_ID = testData.users.getFirstId();
Long ITEM_ID = testData.items.getFirstId();
UserTransaction tx = TM.getUserTransaction();
try {
Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
// Clear all natural ID cache regions
JPA.getEntityManagerFactory().getCache().unwrap(org.hibernate.Cache.class).evictNaturalIdRegions();
// Clear the User entity cache region
JPA.getEntityManagerFactory().getCache().evict(User.class);
{
tx.begin();
EntityManager em = JPA.createEntityManager();
Session session = em.unwrap(Session.class);
NaturalIdCacheStatistics userIdStats = stats.getNaturalIdCacheStatistics(User.class.getName() + "##NaturalId");
assertEquals(userIdStats.getElementCountInMemory(), 0);
User user = (User) session.byNaturalId(User.class).using("username", "johndoe").load();
// select ID from USERS where USERNAME = ?
// select * from USERS where ID = ?
assertNotNull(user);
assertEquals(userIdStats.getHitCount(), 0);
assertEquals(userIdStats.getMissCount(), 1);
assertEquals(userIdStats.getElementCountInMemory(), 1);
SecondLevelCacheStatistics userStats = stats.getSecondLevelCacheStatistics(User.class.getName());
assertEquals(userStats.getHitCount(), 0);
assertEquals(userStats.getMissCount(), 1);
assertEquals(userStats.getElementCountInMemory(), 1);
tx.commit();
em.close();
}
{
// Execute the lookup again, hit the cache
tx.begin();
EntityManager em = JPA.createEntityManager();
Session session = em.unwrap(Session.class);
/*
The natural identifier cache region for <code>User</code>s
has one element.
*/
NaturalIdCacheStatistics userIdStats = stats.getNaturalIdCacheStatistics(User.class.getName() + "##NaturalId");
assertEquals(userIdStats.getElementCountInMemory(), 1);
/*
The <code>org.hibernate.Session</code> API performs natural
identifier lookup; this is the only API for accessing the
natural identifier cache.
*/
User user = (User) session.byNaturalId(User.class).using("username", "johndoe").load();
assertNotNull(user);
/*
You had a cache hit for the natural identifier lookup; the
cache returned the identifier value of "johndoe".
*/
assertEquals(userIdStats.getHitCount(), 1);
/*
You also had a cache hit for the actual entity data of
that <code>User</code>.
*/
SecondLevelCacheStatistics userStats = stats.getSecondLevelCacheStatistics(User.class.getName());
assertEquals(userStats.getHitCount(), 1);
tx.commit();
em.close();
}
} finally {
TM.rollback();
}
}
use of javax.persistence.Cache in project OpenOLAT by OpenOLAT.
the class DBImpl method getCacheContainer.
@Override
public EmbeddedCacheManager getCacheContainer() {
EmbeddedCacheManager cm;
try {
Cache cache = emf.getCache();
JpaInfinispanRegionFactory region = cache.unwrap(JpaInfinispanRegionFactory.class);
cm = region.getCacheManager();
} catch (Exception e) {
log.error("", e);
cm = null;
}
return cm;
}
use of javax.persistence.Cache in project openolat by klemens.
the class DBImpl method getCacheContainer.
@Override
public EmbeddedCacheManager getCacheContainer() {
EmbeddedCacheManager cm;
try {
Cache cache = emf.getCache();
JpaInfinispanRegionFactory region = cache.unwrap(JpaInfinispanRegionFactory.class);
cm = region.getCacheManager();
} catch (Exception e) {
log.error("", e);
cm = null;
}
return cm;
}
Aggregations