use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testAddNewManyToManyPropertyRefNoInitFlushInitLeaveCacheConsistent.
@Test
public void testAddNewManyToManyPropertyRefNoInitFlushInitLeaveCacheConsistent() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
ByRef<Long> otherItemId = new ByRef<>(null);
withTxSession(s -> {
OtherItem otherItem = new OtherItem();
otherItem.setName("steve");
s.save(otherItem);
otherItemId.set(otherItem.getId());
});
// create an element for otherItem.bagOfItems
Item item = new Item();
item.setName("element");
item.setDescription("element Item");
withTxSession(s -> {
OtherItem otherItem = s.get(OtherItem.class, otherItemId.get());
assertFalse(Hibernate.isInitialized(otherItem.getBagOfItems()));
otherItem.addItemToBag(item);
assertFalse(Hibernate.isInitialized(otherItem.getBagOfItems()));
s.persist(item);
s.flush();
Hibernate.initialize(otherItem.getBagOfItems());
markRollbackOnly(s);
});
withTxSession(s -> {
OtherItem otherItem = s.get(OtherItem.class, otherItemId.get());
Hibernate.initialize(otherItem.getBagOfItems());
assertTrue(otherItem.getBagOfItems().isEmpty());
s.delete(otherItem);
});
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testQueryCacheInvalidation.
@Test
public void testQueryCacheInvalidation() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
sessionFactory().getCache().evictEntityRegion(Item.class.getName());
TIME_SERVICE.advance(1);
assertEquals(0, slcs.getPutCount());
assertEquals(0, slcs.getElementCountInMemory());
assertEquals(0, slcs.getEntries().size());
ByRef<Long> idRef = new ByRef<>(null);
withTxSession(s -> {
Item item = new Item();
item.setName("widget");
item.setDescription("A really top-quality, full-featured widget.");
s.persist(item);
idRef.set(item.getId());
});
assertEquals(1, slcs.getPutCount());
assertEquals(1, slcs.getElementCountInMemory());
assertEquals(1, slcs.getEntries().size());
withTxSession(s -> {
Item item = s.get(Item.class, idRef.get());
assertEquals(slcs.getHitCount(), 1);
assertEquals(slcs.getMissCount(), 0);
item.setDescription("A bog standard item");
});
assertEquals(slcs.getPutCount(), 2);
CacheEntry entry = (CacheEntry) slcs.getEntries().get(idRef.get());
Serializable[] ser = entry.getDisassembledState();
assertTrue(ser[0].equals("widget"));
assertTrue(ser[1].equals("A bog standard item"));
withTxSession(s -> {
Item item = s.load(Item.class, idRef.get());
s.delete(item);
});
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class JndiRegionFactoryTest method addEntityCheckCache.
private void addEntityCheckCache(SessionFactoryImplementor sessionFactory) throws Exception {
Item item = new Item("chris", "Chris's Item");
withTxSession(s -> s.persist(item));
withTxSession(s -> {
Item found = s.load(Item.class, item.getId());
Statistics stats = sessionFactory.getStatistics();
log.info(stats.toString());
assertEquals(item.getDescription(), found.getDescription());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(1, stats.getSecondLevelCacheHitCount());
s.delete(found);
});
}
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);
}
Aggregations