Search in sources :

Example 1 with Statistics

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();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) UserTransaction(javax.transaction.UserTransaction) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) Statistics(org.hibernate.stat.Statistics) NameNotFoundException(javax.naming.NameNotFoundException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with Statistics

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);
}
Also used : SessionStatistics(org.hibernate.stat.SessionStatistics) Transaction(org.hibernate.Transaction) SessionStatistics(org.hibernate.stat.SessionStatistics) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with Statistics

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;
}
Also used : CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) Statistics(org.hibernate.stat.Statistics) TabularData(javax.management.openmbean.TabularData)

Example 4 with Statistics

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;
}
Also used : CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) Statistics(org.hibernate.stat.Statistics) TabularData(javax.management.openmbean.TabularData)

Example 5 with Statistics

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);
}
Also used : Person(org.hibernate.test.cache.infinispan.functional.entities.Person) Statistics(org.hibernate.stat.Statistics) Name(org.hibernate.test.cache.infinispan.functional.entities.Name) Test(org.junit.Test)

Aggregations

Statistics (org.hibernate.stat.Statistics)97 Test (org.junit.Test)59 Session (org.hibernate.Session)27 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)26 EntityManager (javax.persistence.EntityManager)21 QueryStatistics (org.hibernate.stat.QueryStatistics)19 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)14 Transaction (org.hibernate.Transaction)12 ArrayList (java.util.ArrayList)11 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 List (java.util.List)10 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)10 SessionFactory (org.hibernate.SessionFactory)9 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)9 ByRef (org.infinispan.commons.util.ByRef)8 TestForIssue (org.hibernate.testing.TestForIssue)6 Date (java.util.Date)5 UserTransaction (javax.transaction.UserTransaction)5 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)5 Test (org.testng.annotations.Test)5