use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.
the class HibernateL2CacheSelfTest method testVersionedEntity.
/**
* @param accessType Cache access type.
* @throws Exception If failed.
*/
private void testVersionedEntity(AccessType accessType) throws Exception {
createSessionFactories(accessType);
try {
Session ses = sesFactory1.openSession();
VersionedEntity e0 = new VersionedEntity(0);
try {
Transaction tx = ses.beginTransaction();
ses.save(e0);
tx.commit();
} finally {
ses.close();
}
ses = sesFactory1.openSession();
long ver;
try {
ver = ((VersionedEntity) ses.load(VersionedEntity.class, 0)).getVersion();
} finally {
ses.close();
}
SecondLevelCacheStatistics stats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(VERSIONED_ENTITY_NAME);
SecondLevelCacheStatistics stats2 = sesFactory2.getStatistics().getSecondLevelCacheStatistics(VERSIONED_ENTITY_NAME);
assertEquals(1, stats1.getElementCountInMemory());
assertEquals(1, stats2.getElementCountInMemory());
ses = sesFactory2.openSession();
try {
assertEquals(ver, ((VersionedEntity) ses.load(VersionedEntity.class, 0)).getVersion());
} finally {
ses.close();
}
assertEquals(1, stats2.getElementCountInMemory());
assertEquals(1, stats2.getHitCount());
if (accessType == AccessType.READ_ONLY)
return;
e0.setVersion(ver - 1);
ses = sesFactory1.openSession();
Transaction tx = ses.beginTransaction();
try {
ses.update(e0);
tx.commit();
fail("Commit must fail.");
} catch (StaleStateException e) {
log.info("Expected exception: " + e);
} finally {
tx.rollback();
ses.close();
}
sesFactory1.getStatistics().clear();
stats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(VERSIONED_ENTITY_NAME);
ses = sesFactory1.openSession();
try {
assertEquals(ver, ((VersionedEntity) ses.load(VersionedEntity.class, 0)).getVersion());
} finally {
ses.close();
}
assertEquals(1, stats1.getElementCountInMemory());
assertEquals(1, stats1.getHitCount());
assertEquals(1, stats2.getElementCountInMemory());
assertEquals(1, stats2.getHitCount());
} finally {
cleanup();
}
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.
the class HibernateL2CacheSelfTest method assertEntityCache.
/**
* @param entityName Entity name.
* @param sesFactory Session factory.
* @param idToName ID to name mapping.
* @param absentIds Absent entities' IDs.
*/
private void assertEntityCache(String entityName, SessionFactory sesFactory, Map<Integer, String> idToName, Integer... absentIds) {
assert entityName.equals(ENTITY_NAME) || entityName.equals(ENTITY2_NAME) : entityName;
sesFactory.getStatistics().clear();
final Session ses = sesFactory.openSession();
final boolean entity1 = entityName.equals(ENTITY_NAME);
try {
if (entity1) {
for (Map.Entry<Integer, String> e : idToName.entrySet()) assertEquals(e.getValue(), ((Entity) ses.load(Entity.class, e.getKey())).getName());
} else {
for (Map.Entry<Integer, String> e : idToName.entrySet()) assertEquals(e.getValue(), ((Entity2) ses.load(Entity2.class, e.getKey())).getName());
}
for (final int id : absentIds) {
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
if (entity1)
((Entity) ses.load(Entity.class, id)).getName();
else
((Entity2) ses.load(Entity2.class, id)).getName();
return null;
}
}, ObjectNotFoundException.class, null);
}
SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(entityName);
assertEquals(idToName.size(), stats.getHitCount());
assertEquals(absentIds.length, stats.getMissCount());
} finally {
ses.close();
}
}
Aggregations