use of org.hibernate.stat.SecondLevelCacheStatistics in project wildfly by wildfly.
the class StatefulBean method getEmployeesInMemory.
@Override
public long getEmployeesInMemory() {
Session session = em.unwrap(Session.class);
String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
for (String name : entityRegionNames) {
if (name.contains(Employee.class.getName())) {
SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(name);
return stats.getElementCountInMemory();
}
}
return -1;
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class HibernateCacheTest method testStaleWritesLeaveCacheConsistent.
@Test
public void testStaleWritesLeaveCacheConsistent() {
Session s = sessionFactory().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(item.getVersion() - 1);
try {
s = sessionFactory().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(REGION_PREFIX + VersionedItem.class.getName());
assertNotNull(slcs);
final Map entries = slcs.getEntries();
Object entry = entries.get(item.getId());
Long cachedVersionValue;
if (entry instanceof SoftLock) {
//FIXME don't know what to test here
//cachedVersionValue = new Long( ( (ReadWriteCache.Lock) entry).getUnlockTimestamp() );
} else {
cachedVersionValue = (Long) ((Map) entry).get("_version");
assertThat(initialVersion, equalTo(cachedVersionValue));
}
// cleanup
s = sessionFactory().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 ignite by apache.
the class HibernateL2CacheExample method printStats.
/**
* Prints Hibernate L2 cache statistics to standard output.
*
* @param sesFactory Hibernate {@link SessionFactory}, for which to print
* statistics.
*/
private static void printStats(SessionFactory sesFactory) {
System.out.println("=== Hibernate L2 cache statistics ===");
for (String entityName : ENTITY_NAMES) {
System.out.println("\tEntity: " + entityName);
SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(entityName);
System.out.println("\t\tPuts: " + stats.getPutCount());
System.out.println("\t\tHits: " + stats.getHitCount());
System.out.println("\t\tMisses: " + stats.getMissCount());
}
System.out.println("=====================================");
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.
the class HibernateL2CacheSelfTest method assertCollectionCache.
/**
* @param sesFactory Session factory.
* @param idToChildCnt Number of children per entity.
* @param expHit Expected cache hits.
* @param expMiss Expected cache misses.
*/
@SuppressWarnings("unchecked")
private void assertCollectionCache(SessionFactory sesFactory, Map<Integer, Integer> idToChildCnt, int expHit, int expMiss) {
sesFactory.getStatistics().clear();
Session ses = sesFactory.openSession();
try {
for (Map.Entry<Integer, Integer> e : idToChildCnt.entrySet()) {
Entity entity = (Entity) ses.load(Entity.class, e.getKey());
assertEquals((int) e.getValue(), entity.getChildren().size());
}
} finally {
ses.close();
}
SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(CHILD_COLLECTION_REGION);
assertEquals(expHit, stats.getHitCount());
assertEquals(expMiss, stats.getMissCount());
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.
the class HibernateL2CacheSelfTest method testRegionClear.
/**
* @param accessType Cache access type.
* @throws Exception If failed.
*/
private void testRegionClear(AccessType accessType) throws Exception {
createSessionFactories(accessType);
try {
final int ENTITY_CNT = 100;
Session ses = sesFactory1.openSession();
try {
Transaction tx = ses.beginTransaction();
for (int i = 0; i < ENTITY_CNT; i++) {
Entity e = new Entity(i, "name-" + i);
Collection<ChildEntity> children = new ArrayList<>();
for (int j = 0; j < 3; j++) children.add(new ChildEntity());
e.setChildren(children);
ses.save(e);
}
tx.commit();
} finally {
ses.close();
}
loadEntities(sesFactory2, ENTITY_CNT);
SecondLevelCacheStatistics stats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(ENTITY_NAME);
SecondLevelCacheStatistics stats2 = sesFactory2.getStatistics().getSecondLevelCacheStatistics(ENTITY_NAME);
NaturalIdCacheStatistics idStats1 = sesFactory1.getStatistics().getNaturalIdCacheStatistics(NATURAL_ID_REGION);
NaturalIdCacheStatistics idStats2 = sesFactory2.getStatistics().getNaturalIdCacheStatistics(NATURAL_ID_REGION);
SecondLevelCacheStatistics colStats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(CHILD_COLLECTION_REGION);
SecondLevelCacheStatistics colStats2 = sesFactory2.getStatistics().getSecondLevelCacheStatistics(CHILD_COLLECTION_REGION);
assertEquals(ENTITY_CNT, stats1.getElementCountInMemory());
assertEquals(ENTITY_CNT, stats2.getElementCountInMemory());
assertEquals(ENTITY_CNT, idStats1.getElementCountInMemory());
assertEquals(ENTITY_CNT, idStats2.getElementCountInMemory());
assertEquals(ENTITY_CNT, colStats1.getElementCountInMemory());
assertEquals(ENTITY_CNT, colStats2.getElementCountInMemory());
// Test cache is cleared after update query.
ses = sesFactory1.openSession();
try {
Transaction tx = ses.beginTransaction();
ses.createQuery("delete from " + ENTITY_NAME + " where name='no such name'").executeUpdate();
ses.createQuery("delete from " + ChildEntity.class.getName() + " where id=-1").executeUpdate();
tx.commit();
} finally {
ses.close();
}
assertEquals(0, stats1.getElementCountInMemory());
assertEquals(0, stats2.getElementCountInMemory());
assertEquals(0, idStats1.getElementCountInMemory());
assertEquals(0, idStats2.getElementCountInMemory());
assertEquals(0, colStats1.getElementCountInMemory());
assertEquals(0, colStats2.getElementCountInMemory());
// Load some data in cache.
loadEntities(sesFactory1, 10);
assertEquals(10, stats1.getElementCountInMemory());
assertEquals(10, stats2.getElementCountInMemory());
assertEquals(10, idStats1.getElementCountInMemory());
assertEquals(10, idStats2.getElementCountInMemory());
// Test evictAll method.
sesFactory2.getCache().evictEntityRegion(ENTITY_NAME);
assertEquals(0, stats1.getElementCountInMemory());
assertEquals(0, stats2.getElementCountInMemory());
sesFactory2.getCache().evictNaturalIdRegion(ENTITY_NAME);
assertEquals(0, idStats1.getElementCountInMemory());
assertEquals(0, idStats2.getElementCountInMemory());
sesFactory2.getCache().evictCollectionRegion(CHILD_COLLECTION_REGION);
assertEquals(0, colStats1.getElementCountInMemory());
assertEquals(0, colStats2.getElementCountInMemory());
} finally {
cleanup();
}
}
Aggregations