use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class JBossStandaloneJtaExampleTest method testPersistAndLoadUnderJta.
@Test
public void testPersistAndLoadUnderJta() throws Exception {
Item item;
SessionFactory sessionFactory = buildSessionFactory();
try {
UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");
ut.begin();
try {
Session session = sessionFactory.openSession();
assertTrue(session.getTransaction().isActive());
item = new Item("anItem", "An item owned by someone");
session.persist(item);
// IMO the flush should not be necessary, but session.close() does not flush
// and the item is not persisted.
session.flush();
session.close();
} catch (Exception e) {
ut.setRollbackOnly();
throw e;
} finally {
if (ut.getStatus() == Status.STATUS_ACTIVE)
ut.commit();
else
ut.rollback();
}
ut = (UserTransaction) ctx.lookup("UserTransaction");
ut.begin();
try {
Session session = sessionFactory.openSession();
assertTrue(session.getTransaction().isActive());
Item found = (Item) session.load(Item.class, item.getId());
Statistics stats = session.getSessionFactory().getStatistics();
log.info(stats.toString());
assertEquals(item.getDescription(), found.getDescription());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(1, stats.getSecondLevelCacheHitCount());
session.delete(found);
// IMO the flush should not be necessary, but session.close() does not flush
// and the item is not deleted.
session.flush();
session.close();
} catch (Exception e) {
ut.setRollbackOnly();
throw e;
} finally {
if (ut.getStatus() == Status.STATUS_ACTIVE)
ut.commit();
else
ut.rollback();
}
ut = (UserTransaction) ctx.lookup("UserTransaction");
ut.begin();
try {
Session session = sessionFactory.openSession();
assertTrue(session.getTransaction().isActive());
assertNull(session.get(Item.class, item.getId()));
session.close();
} catch (Exception e) {
ut.setRollbackOnly();
throw e;
} finally {
if (ut.getStatus() == Status.STATUS_ACTIVE)
ut.commit();
else
ut.rollback();
}
} finally {
if (sessionFactory != null)
sessionFactory.close();
}
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class SessionStatsTest method testSessionStatistics.
@Test
public void testSessionStatistics() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Statistics stats = sessionFactory().getStatistics();
stats.clear();
boolean isStats = stats.isStatisticsEnabled();
stats.setStatisticsEnabled(true);
Continent europe = fillDb(s);
tx.commit();
s.clear();
tx = s.beginTransaction();
SessionStatistics sessionStats = s.getStatistics();
assertEquals(0, sessionStats.getEntityKeys().size());
assertEquals(0, sessionStats.getEntityCount());
assertEquals(0, sessionStats.getCollectionKeys().size());
assertEquals(0, sessionStats.getCollectionCount());
europe = (Continent) s.get(Continent.class, europe.getId());
Hibernate.initialize(europe.getCountries());
Hibernate.initialize(europe.getCountries().iterator().next());
assertEquals(2, sessionStats.getEntityKeys().size());
assertEquals(2, sessionStats.getEntityCount());
assertEquals(1, sessionStats.getCollectionKeys().size());
assertEquals(1, sessionStats.getCollectionCount());
tx.commit();
s.close();
stats.setStatisticsEnabled(isStats);
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class HibernateStatsImpl method getQueryStats.
@Override
public TabularData getQueryStats() {
final List<CompositeData> result = new ArrayList<CompositeData>();
final Statistics statistics = getStatistics();
for (String query : statistics.getQueries()) {
final QueryStats queryStats = new QueryStats(query, statistics.getQueryStatistics(query));
result.add(queryStats.toCompositeData());
}
final TabularData td = QueryStats.newTabularDataInstance();
td.putAll(result.toArray(new CompositeData[result.size()]));
return td;
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class HibernateStatsImpl method getCacheRegionStats.
@Override
public TabularData getCacheRegionStats() {
final List<CompositeData> list = new ArrayList<CompositeData>();
final Statistics statistics = getStatistics();
for (String region : statistics.getSecondLevelCacheRegionNames()) {
final CacheRegionStats l2CacheStats = new CacheRegionStats(region, statistics.getSecondLevelCacheStatistics(region));
list.add(l2CacheStats.toCompositeData());
}
final TabularData td = CacheRegionStats.newTabularDataInstance();
td.putAll(list.toArray(new CompositeData[list.size()]));
return td;
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class EqualityTest method testEqualityFromType.
@Test
public void testEqualityFromType() throws Exception {
Person john = new Person("John", "Black", 26);
Person peter = new Person("Peter", "White", 32);
withTxSession(s -> {
s.persist(john);
s.persist(peter);
});
Statistics statistics = sessionFactory().getStatistics();
statistics.clear();
for (int i = 0; i < 5; ++i) {
withTxSession(s -> {
Person p1 = s.get(Person.class, john.getName());
assertPersonEquals(john, p1);
Person p2 = s.get(Person.class, peter.getName());
assertPersonEquals(peter, p2);
Person p3 = s.get(Person.class, new Name("Foo", "Bar"));
assertNull(p3);
});
}
assertTrue(statistics.getSecondLevelCacheHitCount() > 0);
assertTrue(statistics.getSecondLevelCacheMissCount() > 0);
}
Aggregations