use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class EhCacheTest method testQueryCacheInvalidation.
@Test
public void testQueryCacheInvalidation() {
Session s = openSession();
Transaction t = s.beginTransaction();
Item i = new Item();
i.setName("widget");
i.setDescription("A really top-quality, full-featured widget.");
s.persist(i);
t.commit();
s.close();
SecondLevelCacheStatistics slcs = s.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(Item.class.getName());
assertEquals(slcs.getPutCount(), 1);
assertEquals(slcs.getElementCountInMemory(), 1);
assertEquals(slcs.getEntries().size(), 1);
s = openSession();
t = s.beginTransaction();
i = (Item) s.get(Item.class, i.getId());
assertEquals(slcs.getHitCount(), 1);
assertEquals(slcs.getMissCount(), 0);
i.setDescription("A bog standard item");
t.commit();
s.close();
assertEquals(slcs.getPutCount(), 2);
Object entry = slcs.getEntries().get(i.getId());
Map map;
map = getMapFromCacheEntry(entry);
assertTrue(map.get("description").equals("A bog standard item"));
assertTrue(map.get("name").equals("widget"));
// cleanup
s = openSession();
t = s.beginTransaction();
s.delete(i);
t.commit();
s.close();
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class EhCacheTest method testStaleWritesLeaveCacheConsistent.
@SuppressWarnings({ "UnnecessaryBoxing", "UnnecessaryUnboxing", "UnusedAssignment" })
@Test
public void testStaleWritesLeaveCacheConsistent() {
Session s = openSession();
Transaction txn = s.beginTransaction();
VersionedItem item = new VersionedItem();
item.setName("steve");
item.setDescription("steve's item");
s.save(item);
txn.commit();
s.close();
Long initialVersion = item.getVersion();
// manually revert the version property
item.setVersion(Long.valueOf(item.getVersion().longValue() - 1));
try {
s = openSession();
txn = s.beginTransaction();
s.update(item);
txn.commit();
s.close();
fail("expected stale write to fail");
} catch (Throwable expected) {
// expected behavior here
if (txn != null) {
try {
txn.rollback();
} catch (Throwable ignore) {
}
}
} finally {
if (s != null && s.isOpen()) {
try {
s.close();
} catch (Throwable ignore) {
}
}
}
// check the version value in the cache...
SecondLevelCacheStatistics slcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(VersionedItem.class.getName());
Object entry = slcs.getEntries().get(item.getId());
Long cachedVersionValue;
// } else
if (entry.getClass().getName().equals("org.hibernate.cache.ehcache.internal.strategy.AbstractReadWriteEhcacheAccessStrategy$Lock")) {
//FIXME don't know what to test here
} else {
cachedVersionValue = (Long) getMapFromCacheEntry(entry).get("_version");
assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
}
// cleanup
s = openSession();
txn = s.beginTransaction();
item = (VersionedItem) s.load(VersionedItem.class, item.getId());
s.delete(item);
txn.commit();
s.close();
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class SecondLevelCacheTest method testCache.
@Test
public void testCache() {
doInJPA(this::entityManagerFactory, entityManager -> {
entityManager.persist(new Person());
entityManager.persist(new Person());
Person aPerson = new Person();
aPerson.setName("John Doe");
aPerson.setCode("unique-code");
entityManager.persist(aPerson);
return aPerson;
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa load by id");
Person person = entityManager.find(Person.class, 1L);
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native load by id");
Session session = entityManager.unwrap(Session.class);
Person person = session.get(Person.class, 1L);
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native load by natural-id");
Session session = entityManager.unwrap(Session.class);
Person person = session.byNaturalId(Person.class).using("code", "unique-code").load();
assertNotNull(person);
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa query cache");
List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.name = :name", Person.class).setParameter("name", "John Doe").setHint("org.hibernate.cacheable", "true").getResultList();
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache");
Session session = entityManager.unwrap(Session.class);
List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.name = :name").setParameter("name", "John Doe").setCacheable(true).list();
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa query cache region");
List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").getResultList();
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache");
Session session = entityManager.unwrap(Session.class);
List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").list();
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Jpa query cache store mode ");
List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache store mode");
Session session = entityManager.unwrap(Session.class);
List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").setCacheMode(CacheMode.REFRESH).list();
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
Statistics statistics = session.getSessionFactory().getStatistics();
SecondLevelCacheStatistics secondLevelCacheStatistics = statistics.getSecondLevelCacheStatistics("query.cache.person");
long hitCount = secondLevelCacheStatistics.getHitCount();
long missCount = secondLevelCacheStatistics.getMissCount();
double hitRatio = (double) hitCount / (hitCount + missCount);
return hitRatio;
});
doInJPA(this::entityManagerFactory, entityManager -> {
log.info("Native query cache store mode");
Session session = entityManager.unwrap(Session.class);
session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
});
doInJPA(this::entityManagerFactory, entityManager -> {
Map<String, Object> hints = new HashMap<>();
hints.put("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE);
hints.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
Person person = entityManager.find(Person.class, 1L, hints);
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.setCacheMode(CacheMode.REFRESH);
Person person = session.get(Person.class, 1L);
});
doInJPA(this::entityManagerFactory, entityManager -> {
List<Person> persons = entityManager.createQuery("select p from Person p", Person.class).setHint(QueryHints.HINT_CACHEABLE, "true").setHint("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE).setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
entityManager.getEntityManagerFactory().getCache().evict(Person.class);
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
List<Person> persons = session.createQuery("select p from Person p").setCacheable(true).setCacheMode(CacheMode.REFRESH).list();
session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
});
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class HibernateCacheTest method testQueryCacheInvalidation.
@Test
public void testQueryCacheInvalidation() throws Exception {
Session s = sessionFactory().openSession();
Transaction t = s.beginTransaction();
Item i = new Item();
i.setName("widget");
i.setDescription("A really top-quality, full-featured widget.");
s.persist(i);
t.commit();
s.close();
SecondLevelCacheStatistics slcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(REGION_PREFIX + Item.class.getName());
assertThat(slcs.getPutCount(), equalTo(1L));
assertThat(slcs.getEntries().size(), equalTo(1));
s = sessionFactory().openSession();
t = s.beginTransaction();
i = (Item) s.get(Item.class, i.getId());
assertThat(slcs.getHitCount(), equalTo(1L));
assertThat(slcs.getMissCount(), equalTo(0L));
i.setDescription("A bog standard item");
t.commit();
s.close();
assertThat(slcs.getPutCount(), equalTo(2L));
Object entry = slcs.getEntries().get(i.getId());
Map map;
if (entry instanceof Map) {
map = (Map) entry;
} else {
map = ItemValueExtractor.getValue(entry);
}
assertThat((String) map.get("description"), equalTo("A bog standard item"));
assertThat((String) map.get("name"), equalTo("widget"));
// cleanup
s = sessionFactory().openSession();
t = s.beginTransaction();
s.delete(i);
t.commit();
s.close();
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class HibernateCacheTest method testEmptySecondLevelCacheEntry.
@Test
public void testEmptySecondLevelCacheEntry() throws Exception {
sessionFactory().getCache().evictEntityRegion(Item.class.getName());
Statistics stats = sessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics(REGION_PREFIX + Item.class.getName());
Map cacheEntries = statistics.getEntries();
assertThat(cacheEntries.size(), equalTo(0));
}
Aggregations