Search in sources :

Example 1 with CollectionStatistics

use of org.hibernate.stat.CollectionStatistics in project hibernate-orm by hibernate.

the class PersistentSetTest method testCollectiondirtyChecking.

@Test
public void testCollectiondirtyChecking() {
    Session session = openSession();
    session.beginTransaction();
    Parent parent = new Parent("p1");
    Child child = new Child("c1");
    parent.getChildren().add(child);
    child.setParent(parent);
    session.save(parent);
    session.getTransaction().commit();
    session.close();
    CollectionStatistics stats = sessionFactory().getStatistics().getCollectionStatistics(Parent.class.getName() + ".children");
    long recreateCount = stats.getRecreateCount();
    long updateCount = stats.getUpdateCount();
    session = openSession();
    session.beginTransaction();
    parent = (Parent) session.get(Parent.class, "p1");
    assertEquals(1, parent.getChildren().size());
    session.getTransaction().commit();
    session.close();
    assertEquals(1, parent.getChildren().size());
    assertEquals(recreateCount, stats.getRecreateCount());
    assertEquals(updateCount, stats.getUpdateCount());
    session = openSession();
    session.beginTransaction();
    assertEquals(1, parent.getChildren().size());
    session.delete(parent);
    session.getTransaction().commit();
    session.close();
}
Also used : Session(org.hibernate.Session) CollectionStatistics(org.hibernate.stat.CollectionStatistics) Test(org.junit.Test)

Example 2 with CollectionStatistics

use of org.hibernate.stat.CollectionStatistics in project hibernate-orm by hibernate.

the class BatchedManyToManyTest method testLoadingNonInverseSide.

@Test
public void testLoadingNonInverseSide() {
    prepareTestData();
    sessionFactory().getStatistics().clear();
    CollectionStatistics userGroupStats = sessionFactory().getStatistics().getCollectionStatistics(User.class.getName() + ".groups");
    CollectionStatistics groupUserStats = sessionFactory().getStatistics().getCollectionStatistics(Group.class.getName() + ".users");
    Interceptor testingInterceptor = new EmptyInterceptor() {

        @Override
        public String onPrepareStatement(String sql) {
            // ugh, this is the best way I could come up with to assert this.
            // unfortunately, this is highly dependent on the dialect and its
            // outer join fragment.  But at least this wil fail on the majority
            // of dialects...
            Assert.assertFalse("batch load of many-to-many should use inner join", sql.toLowerCase(Locale.ROOT).contains("left outer join"));
            return super.onPrepareStatement(sql);
        }
    };
    Session s = openSession(testingInterceptor);
    s.beginTransaction();
    List users = s.createQuery("from User u").list();
    User user = (User) users.get(0);
    assertTrue(Hibernate.isInitialized(user));
    assertTrue(Hibernate.isInitialized(user.getGroups()));
    user = (User) users.get(1);
    assertTrue(Hibernate.isInitialized(user));
    assertTrue(Hibernate.isInitialized(user.getGroups()));
    // should have been just one fetch (the batch fetch)
    assertEquals(1, userGroupStats.getFetchCount());
    // should have been just one fetch (the batch fetch)
    assertEquals(1, groupUserStats.getFetchCount());
    s.getTransaction().commit();
    s.close();
}
Also used : List(java.util.List) EmptyInterceptor(org.hibernate.EmptyInterceptor) Interceptor(org.hibernate.Interceptor) EmptyInterceptor(org.hibernate.EmptyInterceptor) CollectionStatistics(org.hibernate.stat.CollectionStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with CollectionStatistics

use of org.hibernate.stat.CollectionStatistics in project bw-calendar-engine by Bedework.

the class DbStatistics method collectionStats.

private static void collectionStats(Collection<StatsEntry> c, Statistics dbStats, Class cl, String cname) {
    String name = cl.getName() + "." + cname;
    c.add(new StatsEntry("Statistics for " + name));
    CollectionStatistics cStats = dbStats.getCollectionStatistics(name);
    c.add(new StatsEntry("Fetched", cStats.getFetchCount()));
    c.add(new StatsEntry("Loaded", cStats.getLoadCount()));
    c.add(new StatsEntry("Recreated", cStats.getRecreateCount()));
    c.add(new StatsEntry("Removed", cStats.getRemoveCount()));
    c.add(new StatsEntry("Updated", cStats.getUpdateCount()));
}
Also used : StatsEntry(org.bedework.calfacade.BwStats.StatsEntry) CollectionStatistics(org.hibernate.stat.CollectionStatistics)

Example 4 with CollectionStatistics

use of org.hibernate.stat.CollectionStatistics in project Gemma by PavlidisLab.

the class HibernateMonitorImpl method getStats.

@Override
public String getStats(boolean showEntityStats, boolean showCollectionStats, boolean showSecondLevelCacheDetails) {
    Statistics stats = sessionFactory.getStatistics();
    StringBuilder buf = new StringBuilder();
    buf.append("Statistics started at: ").append(new Date(stats.getStartTime())).append("\n");
    long flushes = stats.getFlushCount();
    long trans = stats.getTransactionCount();
    long prep = stats.getPrepareStatementCount();
    long open = stats.getSessionOpenCount();
    long close = stats.getSessionCloseCount();
    long ex = stats.getQueryExecutionCount();
    buf.append("Queries executed: ").append(ex).append("\n");
    buf.append(open).append(" sessions opened, ").append(close).append(" closed\n");
    buf.append(prep).append(" statements prepared, ").append(trans).append(" transactions completed, ").append(flushes).append(" flushes.\n");
    String slowQuery = stats.getQueryExecutionMaxTimeQueryString();
    long queryExecutionMaxTime = stats.getQueryExecutionMaxTime();
    if (queryExecutionMaxTime > 1000) {
        buf.append("Slowest query [").append(queryExecutionMaxTime).append("ms]: ").append(StringUtils.abbreviate(slowQuery, 150)).append("\n");
    }
    buf.append("\n------------------- Query Cache stats -----------------------\n");
    long queryCacheHitCount = stats.getQueryCacheHitCount();
    long queryCacheMissCount = stats.getQueryCacheMissCount();
    long queryCachePutCount = stats.getQueryCachePutCount();
    buf.append("Puts: ").append(queryCachePutCount).append("\n");
    buf.append("Hits: ").append(queryCacheHitCount).append("\n");
    buf.append("Misses: ").append(queryCacheMissCount).append("\n");
    buf.append("\n------------------- Second Level Cache stats -----------------------\n");
    long secCacheHits = stats.getSecondLevelCacheHitCount();
    long secCacheMiss = stats.getSecondLevelCacheMissCount();
    long secCachePut = stats.getSecondLevelCachePutCount();
    buf.append("Puts: ").append(secCachePut).append("\n");
    buf.append("Hits: ").append(secCacheHits).append("\n");
    buf.append("Misses: ").append(secCacheMiss).append("\n");
    if (showSecondLevelCacheDetails) {
        String[] regions = stats.getSecondLevelCacheRegionNames();
        Arrays.sort(regions);
        for (String region : regions) {
            SecondLevelCacheStatistics secondLevelCacheStatistics = stats.getSecondLevelCacheStatistics(region);
            long hitCount = secondLevelCacheStatistics.getHitCount();
            long missCount = secondLevelCacheStatistics.getMissCount();
            long putCount = secondLevelCacheStatistics.getPutCount();
            long size = secondLevelCacheStatistics.getSizeInMemory();
            long count = secondLevelCacheStatistics.getElementCountInMemory();
            long diskCount = secondLevelCacheStatistics.getElementCountOnDisk();
            if (putCount > 0 || hitCount > 0 || missCount > 0) {
                buf.append(region).append(": ").append(hitCount).append(" hits; ").append(missCount).append(" misses; ").append(putCount).append(" puts; Memcount=").append(count).append("; Diskcount=").append(diskCount).append(" MemSizeBytes=").append(size).append("\n");
            }
        }
    }
    if (showCollectionStats) {
        buf.append("\n------------------- Collection stats -----------------------\n");
        String[] collectionRoleNames = stats.getCollectionRoleNames();
        Arrays.sort(collectionRoleNames);
        for (String string : collectionRoleNames) {
            CollectionStatistics collectionStatistics = stats.getCollectionStatistics(string);
            long fetchCount = collectionStatistics.getFetchCount();
            long loadCount = collectionStatistics.getLoadCount();
            long updateCount = collectionStatistics.getUpdateCount();
            if (fetchCount > 0 || loadCount > 0 || updateCount > 0) {
                buf.append(string).append(": ").append(fetchCount).append(" fetches, ").append(loadCount).append(" loads, ").append(updateCount).append(" updates\n");
            }
        }
    }
    if (showEntityStats) {
        buf.append("\n------------------- Entity stats -----------------------\n");
        String[] entityNames = stats.getEntityNames();
        Arrays.sort(entityNames);
        for (String string : entityNames) {
            EntityStatistics entityStats = stats.getEntityStatistics(string);
            long changes = entityStats.getInsertCount() + entityStats.getUpdateCount() + entityStats.getDeleteCount();
            if (changes > 0) {
                String shortName;
                try {
                    shortName = Class.forName(string).getSimpleName().replaceFirst("Impl", "");
                    buf.append(shortName).append(" updates: ").append(changes).append(" \n");
                } catch (ClassNotFoundException e) {
                    HibernateMonitorImpl.log.error(e, e);
                }
            }
            long reads = entityStats.getLoadCount();
            if (reads > 0) {
                String shortName;
                try {
                    shortName = Class.forName(string).getSimpleName().replaceFirst("Impl", "");
                    buf.append(shortName).append(" read: ").append(reads).append(" \n");
                } catch (ClassNotFoundException e) {
                    HibernateMonitorImpl.log.error(e, e);
                }
            }
        }
    }
    return buf.toString();
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) CollectionStatistics(org.hibernate.stat.CollectionStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) EntityStatistics(org.hibernate.stat.EntityStatistics) EntityStatistics(org.hibernate.stat.EntityStatistics) Date(java.util.Date) CollectionStatistics(org.hibernate.stat.CollectionStatistics)

Example 5 with CollectionStatistics

use of org.hibernate.stat.CollectionStatistics in project hibernate-orm by hibernate.

the class PersistentSetTest method testCompositeElementMerging.

@Test
@FailureExpected(jiraKey = "HHH-2485")
public void testCompositeElementMerging() {
    Session session = openSession();
    session.beginTransaction();
    Container container = new Container("p1");
    Container.Content c1 = new Container.Content("c1");
    container.getContents().add(c1);
    session.save(container);
    session.getTransaction().commit();
    session.close();
    CollectionStatistics stats = sessionFactory().getStatistics().getCollectionStatistics(Container.class.getName() + ".contents");
    long recreateCount = stats.getRecreateCount();
    long updateCount = stats.getUpdateCount();
    container.setName("another name");
    session = openSession();
    session.beginTransaction();
    container = (Container) session.merge(container);
    session.getTransaction().commit();
    session.close();
    assertEquals(1, container.getContents().size());
    assertEquals(recreateCount, stats.getRecreateCount());
    assertEquals(updateCount, stats.getUpdateCount());
    session = openSession();
    session.beginTransaction();
    container = (Container) session.get(Container.class, container.getId());
    assertEquals(1, container.getContents().size());
    session.delete(container);
    session.getTransaction().commit();
    session.close();
}
Also used : Session(org.hibernate.Session) CollectionStatistics(org.hibernate.stat.CollectionStatistics) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Aggregations

CollectionStatistics (org.hibernate.stat.CollectionStatistics)8 Session (org.hibernate.Session)5 Test (org.junit.Test)5 FailureExpected (org.hibernate.testing.FailureExpected)2 Date (java.util.Date)1 List (java.util.List)1 StatsEntry (org.bedework.calfacade.BwStats.StatsEntry)1 EmptyInterceptor (org.hibernate.EmptyInterceptor)1 Interceptor (org.hibernate.Interceptor)1 EntityStatistics (org.hibernate.stat.EntityStatistics)1 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)1 Statistics (org.hibernate.stat.Statistics)1