use of org.hibernate.stat.Statistics in project wildfly by wildfly.
the class Hibernate2LCacheStatsTestCase method testHibernateStatistics.
@Test
public void testHibernateStatistics() throws Exception {
SFSBHibernate2LcacheStats sfsb = lookup("SFSBHibernate2LcacheStats", SFSBHibernate2LcacheStats.class);
// setup Configuration and SessionFactory
sfsb.setupConfig();
try {
Set<Satellite> satellites1 = new HashSet<Satellite>();
Satellite sat = new Satellite();
sat.setId(new Integer(1));
sat.setName("MOON");
satellites1.add(sat);
Planet s1 = sfsb.prepareData("EARTH", "MILKY WAY", "SUN", satellites1, new Integer(1));
Planet s2 = sfsb.getPlanet(s1.getPlanetId());
DataSource ds = rawLookup("java:jboss/datasources/ExampleDS", DataSource.class);
Connection conn = ds.getConnection();
int updated = conn.prepareStatement("update PLANET set galaxy_name='ANDROMEDA' where planetId=1").executeUpdate();
assertTrue("was able to update added Planet. update count=" + updated, updated > 0);
conn.close();
// read updated (dirty) data from second level cache
s2 = sfsb.getPlanet(s2.getPlanetId());
assertTrue("was able to read updated Planet entity", s2 != null);
assertEquals("Galaxy for Planet " + s2.getPlanetName() + " was read from second level cache = " + s2.getGalaxy(), "MILKY WAY", s2.getGalaxy());
assertEquals(s2.getSatellites().size(), 1);
Statistics stats = sfsb.getStatistics();
assertEquals(stats.getCollectionLoadCount(), 1);
assertEquals(stats.getEntityLoadCount(), 2);
assertEquals(stats.getSecondLevelCacheHitCount(), 1);
// Collection in secondlevel cache before eviction
assertTrue(sfsb.isSatellitesPresentInCache(1));
Statistics statsAfterEviction = sfsb.getStatisticsAfterEviction();
// Collection in secondlevel cache after eviction
assertFalse(sfsb.isSatellitesPresentInCache(1));
} finally {
sfsb.cleanup();
}
}
use of org.hibernate.stat.Statistics in project wildfly by wildfly.
the class SFSBHibernate2LcacheStats method getStatisticsAfterEviction.
// fetch statistics after eviction of collection from cache
public Statistics getStatisticsAfterEviction() {
sessionFactory.getCache().evictCollection(org.jboss.as.test.integration.hibernate.Planet.class.getName() + ".satellites", new Integer(1));
Statistics sessionStats = sessionFactory.getStatistics();
return sessionStats;
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class CascadePersistTest method testLazyCollectionsStayLazyOnPersist.
@Test
public void testLazyCollectionsStayLazyOnPersist() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
// initialize
A a = new A();
a.setName("name1");
em.persist(a);
a = new A();
a.setName("name2");
em.persist(a);
a = new A();
a.setName("name3");
em.persist(a);
em.flush();
a = em.find(A.class, 1);
for (int i = 0; i < 3; i++) {
B1 b1 = new B1();
b1.setA(a);
em.persist(b1);
}
for (int i = 0; i < 3; i++) {
B2 b2 = new B2();
b2.setA(a);
em.persist(b2);
}
for (int i = 0; i < 3; i++) {
B3 b3 = new B3();
b3.setA(a);
em.persist(b3);
}
for (int i = 0; i < 3; i++) {
B4 b4 = new B4();
b4.setA(a);
em.persist(b4);
}
em.flush();
B1 b1 = em.find(B1.class, 1);
for (int i = 0; i < 2; i++) {
C1 c1 = new C1();
c1.setB1(b1);
em.persist(c1);
}
B2 b2 = em.find(B2.class, 1);
for (int i = 0; i < 4; i++) {
C2 c2 = new C2();
c2.setB2(b2);
em.persist(c2);
}
em.flush();
em.clear();
// test
a = em.find(A.class, 1);
C2 c2 = new C2();
for (B2 anotherB2 : a.getB2List()) {
if (anotherB2.getId() == 1) {
anotherB2.getC2List().add(c2);
c2.setB2(anotherB2);
}
}
Statistics statistics = em.unwrap(Session.class).getSessionFactory().getStatistics();
statistics.setStatisticsEnabled(true);
statistics.clear();
em.persist(c2);
long loaded = statistics.getEntityLoadCount();
assertEquals(0, loaded);
em.flush();
em.getTransaction().rollback();
em.close();
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class HibernateCacheTest method testGeneralUsage.
@Test
public void testGeneralUsage() {
EventManager mgr = new EventManager(sessionFactory());
Statistics stats = sessionFactory().getStatistics();
// create 3 persons Steve, Orion, Tim
Person stevePerson = new Person();
stevePerson.setFirstname("Steve");
stevePerson.setLastname("Harris");
Long steveId = mgr.createAndStorePerson(stevePerson);
mgr.addEmailToPerson(steveId, "steve@tc.com");
mgr.addEmailToPerson(steveId, "sharrif@tc.com");
mgr.addTalismanToPerson(steveId, "rabbit foot");
mgr.addTalismanToPerson(steveId, "john de conqueroo");
PhoneNumber p1 = new PhoneNumber();
p1.setNumberType("Office");
p1.setPhone(111111);
mgr.addPhoneNumberToPerson(steveId, p1);
PhoneNumber p2 = new PhoneNumber();
p2.setNumberType("Home");
p2.setPhone(222222);
mgr.addPhoneNumberToPerson(steveId, p2);
Person orionPerson = new Person();
orionPerson.setFirstname("Orion");
orionPerson.setLastname("Letizi");
Long orionId = mgr.createAndStorePerson(orionPerson);
mgr.addEmailToPerson(orionId, "orion@tc.com");
mgr.addTalismanToPerson(orionId, "voodoo doll");
Long timId = mgr.createAndStorePerson("Tim", "Teck");
mgr.addEmailToPerson(timId, "teck@tc.com");
mgr.addTalismanToPerson(timId, "magic decoder ring");
Long engMeetingId = mgr.createAndStoreEvent("Eng Meeting", stevePerson, new Date());
mgr.addPersonToEvent(steveId, engMeetingId);
mgr.addPersonToEvent(orionId, engMeetingId);
mgr.addPersonToEvent(timId, engMeetingId);
Long docMeetingId = mgr.createAndStoreEvent("Doc Meeting", orionPerson, new Date());
mgr.addPersonToEvent(steveId, docMeetingId);
mgr.addPersonToEvent(orionId, docMeetingId);
for (Event event : (List<Event>) mgr.listEvents()) {
mgr.listEmailsOfEvent(event.getId());
}
QueryStatistics queryStats = stats.getQueryStatistics("from Event");
assertThat("Cache Miss Count", queryStats.getCacheMissCount(), equalTo(1L));
assertThat("Cache Hit Count", queryStats.getCacheHitCount(), equalTo(0L));
assertThat("Cache Put Count", queryStats.getCachePutCount(), equalTo(1L));
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class CachedQueryTest method testCacheableQuery.
@Test
public void testCacheableQuery() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
for (int i = 0; i < 10; i++) {
Employee employee = new Employee("John" + i, 20d + i);
em.persist(employee);
}
em.getTransaction().commit();
em.close();
HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory) entityManagerFactory();
Statistics stats = hemf.getSessionFactory().getStatistics();
assertEquals(0, stats.getQueryCacheHitCount());
assertEquals(0, stats.getQueryCacheMissCount());
assertEquals(0, stats.getQueryCachePutCount());
assertEquals(0, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(10, stats.getSecondLevelCachePutCount());
stats.clear();
em = getOrCreateEntityManager();
em.getTransaction().begin();
// First time the query is executed, query and results are cached.
TypedQuery<Employee> query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
List<Employee> employees = query.getResultList();
assertEquals(10, employees.size());
assertEquals(0, stats.getQueryCacheHitCount());
assertEquals(1, stats.getQueryCacheMissCount());
assertEquals(1, stats.getQueryCachePutCount());
// the first time the query executes, stats.getSecondLevelCacheHitCount() is 0 because the
// entities are read from the query ResultSet (not from the entity cache).
assertEquals(0, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(0, stats.getSecondLevelCachePutCount());
em.getTransaction().commit();
em.close();
stats.clear();
// Second time the query is executed, list of entities are read from query cache and
// the entities themselves are read from the entity cache.
em = getOrCreateEntityManager();
em.getTransaction().begin();
query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
employees = query.getResultList();
assertEquals(10, employees.size());
assertEquals(1, stats.getQueryCacheHitCount());
assertEquals(0, stats.getQueryCacheMissCount());
assertEquals(0, stats.getQueryCachePutCount());
// the first time the query executes, stats.getSecondLevelCacheHitCount() is 0 because the
// entities are read from the query ResultSet (not from the entity cache).
assertEquals(10, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(0, stats.getSecondLevelCachePutCount());
em.getTransaction().commit();
em.close();
// NOTE: JPACache.evictAll() only evicts entity regions;
// it does not evict the collection regions or query cache region
entityManagerFactory().getCache().evictAll();
stats.clear();
em = getOrCreateEntityManager();
em.getTransaction().begin();
query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
employees = query.getResultList();
assertEquals(10, employees.size());
// query is still found in the cache
assertEquals(1, stats.getQueryCacheHitCount());
assertEquals(0, stats.getQueryCacheMissCount());
assertEquals(0, stats.getQueryCachePutCount());
// since entity regions were evicted, the 10 entities are not found, and are re-put after loading
// as each entity ID is read from the query cache, Hibernate will look the entity up in the
// cache and will not find it; that's why the "miss" and "put" counts are both 10.
assertEquals(0, stats.getSecondLevelCacheHitCount());
assertEquals(10, stats.getSecondLevelCacheMissCount());
assertEquals(10, stats.getSecondLevelCachePutCount());
em.getTransaction().commit();
em.close();
stats.clear();
// this time call clear the entity regions and the query cache region
em = getOrCreateEntityManager();
em.getEntityManagerFactory().getCache().evictAll();
em.unwrap(HibernateEntityManagerImplementor.class).getFactory().getSessionFactory().getCache().evictQueryRegions();
em.getTransaction().begin();
query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
employees = query.getResultList();
assertEquals(10, employees.size());
// query is no longer found in the cache
assertEquals(0, stats.getQueryCacheHitCount());
assertEquals(1, stats.getQueryCacheMissCount());
assertEquals(1, stats.getQueryCachePutCount());
// stats.getSecondLevelCacheHitCount() is 0 because the
// entities are read from the query ResultSet (not from the entity cache).
assertEquals(0, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(10, stats.getSecondLevelCachePutCount());
em.createQuery("delete from Employee").executeUpdate();
em.getTransaction().commit();
em.close();
}
Aggregations