Example usage for org.hibernate.stat Statistics getSecondLevelCacheMissCount

List of usage examples for org.hibernate.stat Statistics getSecondLevelCacheMissCount

Introduction

In this page you can find the example usage for org.hibernate.stat Statistics getSecondLevelCacheMissCount.

Prototype

long getSecondLevelCacheMissCount();

Source Link

Document

Global number of cacheable entities/collections not found in the cache and loaded from the database.

Usage

From source file:com.hazelcast.hibernate.LocalRegionFactoryDefaultTest.java

License:Open Source License

@Test
public void testEntity() {
    final HazelcastInstance hz = getHazelcastInstance(sf);
    assertNotNull(hz);/*w  w w .j a  v  a 2s .com*/
    final int count = 100;
    final int childCount = 3;
    insertDummyEntities(count, childCount);
    List<DummyEntity> list = new ArrayList<DummyEntity>(count);
    Session session = sf.openSession();
    try {
        for (int i = 0; i < count; i++) {
            DummyEntity e = (DummyEntity) session.get(DummyEntity.class, (long) i);
            session.evict(e);
            list.add(e);
        }
    } finally {
        session.close();
    }
    session = sf.openSession();
    Transaction tx = session.beginTransaction();
    try {
        for (DummyEntity dummy : list) {
            dummy.setDate(new Date());
            session.update(dummy);
        }
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

    Statistics stats = sf.getStatistics();
    assertEquals((childCount + 1) * count, stats.getEntityInsertCount());
    // twice put of entity and properties (on load and update) and once put of collection
    assertEquals((childCount + 1) * count * 2 + count, stats.getSecondLevelCachePutCount());
    assertEquals(childCount * count, stats.getEntityLoadCount());
    assertEquals(count, stats.getSecondLevelCacheHitCount());
    // collection cache miss
    assertEquals(count, stats.getSecondLevelCacheMissCount());
    stats.logSummary();
}

From source file:com.hazelcast.hibernate.RegionFactoryDefaultTest.java

License:Open Source License

@Test
public void testEntity() {
    final HazelcastInstance hz = getHazelcastInstance(sf);
    assertNotNull(hz);/* ww w .  ja  v a  2  s  .com*/
    final int count = 100;
    final int childCount = 3;
    insertDummyEntities(count, childCount);
    List<DummyEntity> list = new ArrayList<DummyEntity>(count);
    Session session = sf.openSession();
    try {
        for (int i = 0; i < count; i++) {
            DummyEntity e = (DummyEntity) session.get(DummyEntity.class, (long) i);
            session.evict(e);
            list.add(e);
        }
    } finally {
        session.close();
    }
    session = sf.openSession();
    Transaction tx = session.beginTransaction();
    try {
        for (DummyEntity dummy : list) {
            dummy.setDate(new Date());
            session.update(dummy);
        }
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

    Statistics stats = sf.getStatistics();
    Map<?, ?> cache = hz.getMap(DummyEntity.class.getName());
    Map<?, ?> propCache = hz.getMap(DummyProperty.class.getName());
    Map<?, ?> propCollCache = hz.getMap(DummyEntity.class.getName() + ".properties");
    assertEquals((childCount + 1) * count, stats.getEntityInsertCount());
    // twice put of entity and properties (on load and update) and once put of collection
    // TODO: fix next assertion ->
    //        assertEquals((childCount + 1) * count * 2, stats.getSecondLevelCachePutCount());
    assertEquals(childCount * count, stats.getEntityLoadCount());
    assertEquals(count, stats.getSecondLevelCacheHitCount());
    // collection cache miss
    assertEquals(count, stats.getSecondLevelCacheMissCount());
    assertEquals(count, cache.size());
    assertEquals(count * childCount, propCache.size());
    assertEquals(count, propCollCache.size());
    sf.getCache().evictEntityRegion(DummyEntity.class);
    sf.getCache().evictEntityRegion(DummyProperty.class);
    assertEquals(0, cache.size());
    assertEquals(0, propCache.size());
    stats.logSummary();
}

From source file:com.hazelcast.hibernate.RegionFactoryDefaultTest.java

License:Open Source License

@Test
public void testQuery() {
    final int entityCount = 10;
    final int queryCount = 3;
    insertDummyEntities(entityCount);/* w w w. j a v  a 2 s  . co m*/
    List<DummyEntity> list = null;
    for (int i = 0; i < queryCount; i++) {
        list = executeQuery(sf);
        assertEquals(entityCount, list.size());
    }

    assertNotNull(list);
    Session session = sf.openSession();
    Transaction tx = session.beginTransaction();
    try {
        for (DummyEntity dummy : list) {
            session.delete(dummy);
        }
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

    Statistics stats = sf.getStatistics();
    assertEquals(1, stats.getQueryCachePutCount());
    assertEquals(1, stats.getQueryCacheMissCount());
    assertEquals(queryCount - 1, stats.getQueryCacheHitCount());
    assertEquals(1, stats.getQueryExecutionCount());
    assertEquals(entityCount, stats.getEntityInsertCount());
    //      FIXME
    //      HazelcastRegionFactory puts into L2 cache 2 times; 1 on insert, 1 on query execution
    //      assertEquals(entityCount, stats.getSecondLevelCachePutCount());
    assertEquals(entityCount, stats.getEntityLoadCount());
    assertEquals(entityCount, stats.getEntityDeleteCount());
    assertEquals(entityCount * (queryCount - 1) * 2, stats.getSecondLevelCacheHitCount());
    // collection cache miss
    assertEquals(entityCount, stats.getSecondLevelCacheMissCount());
    stats.logSummary();
}

From source file:com.thoughtworks.go.server.service.support.HibernateInformationProvider.java

License:Apache License

@Override
public Map<String, Object> asJson() {
    LinkedHashMap<String, Object> json = new LinkedHashMap<>();
    Statistics statistics = sessionFactory.getStatistics();
    if (!statistics.isStatisticsEnabled()) {
        return json;
    }// w w  w  .  j  a v a 2 s.c  om
    json.put("EntityDeleteCount", statistics.getEntityDeleteCount());
    json.put("EntityInsertCount", statistics.getEntityInsertCount());
    json.put("EntityLoadCount", statistics.getEntityLoadCount());
    json.put("EntityFetchCount", statistics.getEntityFetchCount());
    json.put("EntityUpdateCount", statistics.getEntityUpdateCount());
    json.put("QueryExecutionCount", statistics.getQueryExecutionCount());
    json.put("QueryExecutionMaxTime", statistics.getQueryExecutionMaxTime());
    json.put("QueryExecutionMaxTimeQueryString", statistics.getQueryExecutionMaxTimeQueryString());
    json.put("QueryCacheHitCount", statistics.getQueryCacheHitCount());
    json.put("QueryCacheMissCount", statistics.getQueryCacheMissCount());
    json.put("QueryCachePutCount", statistics.getQueryCachePutCount());
    json.put("FlushCount", statistics.getFlushCount());
    json.put("ConnectCount", statistics.getConnectCount());
    json.put("SecondLevelCacheHitCount", statistics.getSecondLevelCacheHitCount());
    json.put("SecondLevelCacheMissCount", statistics.getSecondLevelCacheMissCount());
    json.put("SecondLevelCachePutCount", statistics.getSecondLevelCachePutCount());
    json.put("SessionCloseCount", statistics.getSessionCloseCount());
    json.put("SessionOpenCount", statistics.getSessionOpenCount());
    json.put("CollectionLoadCount", statistics.getCollectionLoadCount());
    json.put("CollectionFetchCount", statistics.getCollectionFetchCount());
    json.put("CollectionUpdateCount", statistics.getCollectionUpdateCount());
    json.put("CollectionRemoveCount", statistics.getCollectionRemoveCount());
    json.put("CollectionRecreateCount", statistics.getCollectionRecreateCount());
    json.put("StartTime", statistics.getStartTime());
    json.put("SecondLevelCacheRegionNames", statistics.getSecondLevelCacheRegionNames());
    json.put("SuccessfulTransactionCount", statistics.getSuccessfulTransactionCount());
    json.put("TransactionCount", statistics.getTransactionCount());
    json.put("PrepareStatementCount", statistics.getPrepareStatementCount());
    json.put("CloseStatementCount", statistics.getCloseStatementCount());
    json.put("OptimisticFailureCount", statistics.getOptimisticFailureCount());

    LinkedHashMap<String, Object> queryStats = new LinkedHashMap<>();
    json.put("Queries", queryStats);

    String[] queries = statistics.getQueries();
    for (String query : queries) {
        queryStats.put(query, statistics.getQueryStatistics(query));
    }

    LinkedHashMap<String, Object> entityStatistics = new LinkedHashMap<>();
    json.put("EntityStatistics", entityStatistics);

    String[] entityNames = statistics.getEntityNames();
    for (String entityName : entityNames) {
        entityStatistics.put(entityName, statistics.getEntityStatistics(entityName));
    }

    LinkedHashMap<String, Object> roleStatistics = new LinkedHashMap<>();
    json.put("RoleStatistics", roleStatistics);

    String[] roleNames = statistics.getCollectionRoleNames();
    for (String roleName : roleNames) {
        roleStatistics.put(roleName, statistics.getCollectionStatistics(roleName));
    }

    return json;
}

From source file:de.iew.framework.hibernate.statistics.StatisticsLogger.java

License:Apache License

public void logStatistics() {
    Statistics statistics = this.sessionFactory.getStatistics();
    statistics.setStatisticsEnabled(true);
    StringBuilder sb = new StringBuilder("\nStatistics");
    sb.append("\nCloseStatementCount: ").append(statistics.getCloseStatementCount());

    sb.append("\nEntityDeleteCount: ").append(statistics.getEntityDeleteCount());
    sb.append("\nEntityInsertCount: ").append(statistics.getEntityInsertCount());
    sb.append("\nEntityLoadCount: ").append(statistics.getEntityLoadCount());
    sb.append("\nEntityFetchCount: ").append(statistics.getEntityFetchCount());
    sb.append("\nEntityUpdateCount: ").append(statistics.getEntityUpdateCount());
    sb.append("\nQueryExecutionCount: ").append(statistics.getQueryExecutionCount());
    sb.append("\nQueryExecutionMaxTime: ").append(statistics.getQueryExecutionMaxTime());
    sb.append("\nQueryExecutionMaxTimeQueryString: ").append(statistics.getQueryExecutionMaxTimeQueryString());
    sb.append("\nQueryCacheHitCount: ").append(statistics.getQueryCacheHitCount());
    sb.append("\nQueryCacheMissCount: ").append(statistics.getQueryCacheMissCount());
    sb.append("\nQueryCachePutCount: ").append(statistics.getQueryCachePutCount());
    sb.append("\nNaturalIdQueryExecutionCount: ").append(statistics.getNaturalIdQueryExecutionCount());
    sb.append("\nNaturalIdQueryExecutionMaxTime: ").append(statistics.getNaturalIdQueryExecutionMaxTime());
    sb.append("\nNaturalIdQueryExecutionMaxTimeRegion: ")
            .append(statistics.getNaturalIdQueryExecutionMaxTimeRegion());
    sb.append("\nNaturalIdCacheHitCount: ").append(statistics.getNaturalIdCacheHitCount());
    sb.append("\nNaturalIdCacheMissCount: ").append(statistics.getNaturalIdCacheMissCount());
    sb.append("\nNaturalIdCachePutCount: ").append(statistics.getNaturalIdCachePutCount());
    sb.append("\nUpdateTimestampsCacheHitCount: ").append(statistics.getUpdateTimestampsCacheHitCount());
    sb.append("\nUpdateTimestampsCacheMissCount: ").append(statistics.getUpdateTimestampsCacheMissCount());
    sb.append("\nUpdateTimestampsCachePutCount: ").append(statistics.getUpdateTimestampsCachePutCount());
    sb.append("\nFlushCount: ").append(statistics.getFlushCount());
    sb.append("\nConnectCount: ").append(statistics.getConnectCount());
    sb.append("\nSecondLevelCacheHitCount: ").append(statistics.getSecondLevelCacheHitCount());
    sb.append("\nSecondLevelCacheMissCount: ").append(statistics.getSecondLevelCacheMissCount());
    sb.append("\nSecondLevelCachePutCount: ").append(statistics.getSecondLevelCachePutCount());
    sb.append("\nSessionCloseCount: ").append(statistics.getSessionCloseCount());
    sb.append("\nSessionOpenCount: ").append(statistics.getSessionOpenCount());
    sb.append("\nCollectionLoadCount: ").append(statistics.getCollectionLoadCount());
    sb.append("\nCollectionFetchCount: ").append(statistics.getCollectionFetchCount());
    sb.append("\nCollectionUpdateCount: ").append(statistics.getCollectionUpdateCount());
    sb.append("\nCollectionRemoveCount: ").append(statistics.getCollectionRemoveCount());
    sb.append("\nCollectionRecreateCount: ").append(statistics.getCollectionRecreateCount());
    sb.append("\nStartTime: ").append(statistics.getStartTime());
    sb.append("\nQueries: ").append(statistics.getQueries());
    sb.append("\nEntityNames: ").append(statistics.getEntityNames());
    sb.append("\nCollectionRoleNames: ").append(statistics.getCollectionRoleNames());
    sb.append("\nSecondLevelCacheRegionNames: ").append(statistics.getSecondLevelCacheRegionNames());
    sb.append("\nSuccessfulTransactionCount: ").append(statistics.getSuccessfulTransactionCount());
    sb.append("\nTransactionCount: ").append(statistics.getTransactionCount());
    sb.append("\nPrepareStatementCount: ").append(statistics.getPrepareStatementCount());
    sb.append("\nCloseStatementCount: ").append(statistics.getCloseStatementCount());
    sb.append("\nOptimisticFailureCount: ").append(statistics.getOptimisticFailureCount());

    if (log.isDebugEnabled()) {
        log.debug(sb);//from w  w w . j a  v  a  2s  .co  m
    }

}

From source file:gr.interamerican.bo2.impl.open.hibernate.HibernateBo2Utils.java

License:Open Source License

/**
 * Logs {@link SessionFactory} statistics.
 * /* ww w  .  j  a v  a  2  s  .  c  o m*/
 * @param sessionFactory 
 */
@SuppressWarnings("nls")
public static void logSessionFactoryStatistics(SessionFactory sessionFactory) {
    Statistics statistics = sessionFactory.getStatistics();
    Object[] stats = new Object[] { statistics.getCollectionFetchCount(), statistics.getCollectionLoadCount(),
            statistics.getEntityFetchCount(), statistics.getEntityLoadCount(), statistics.getFlushCount(),
            statistics.getOptimisticFailureCount(), statistics.getQueryExecutionMaxTime(),
            statistics.getQueryExecutionMaxTimeQueryString(), statistics.getSessionOpenCount(),
            statistics.getSecondLevelCacheHitCount(), statistics.getSecondLevelCacheMissCount() };
    StringBuilder sb = new StringBuilder();
    for (Object o : stats) {
        String s = (o == null) ? "null" : o.toString();
        sb.append(s + StringConstants.COMMA + StringConstants.SPACE);
    }
    sb.setLength(sb.length() - 2);
    Debug.debug(logger, "Factory statistics: [ " + sb.toString() + " ]");
}

From source file:models.papmon.HibernateStat.java

License:Open Source License

public HibernateStat(Date refDate) {
    created = refDate;/* w  ww.  j  a  v  a2s.  c  om*/
    Session session = (Session) JPA.em().getDelegate();
    Statistics stats = session.getSessionFactory().getStatistics();
    queryExecutionCount = stats.getQueryExecutionCount();
    queryExecutionMaxTime = stats.getQueryExecutionMaxTime();
    sessionOpenCount = stats.getSessionOpenCount();
    sessionCloseCount = stats.getSessionCloseCount();
    entityLoadCount = stats.getEntityLoadCount();
    entityInsertCount = stats.getEntityInsertCount();
    entityUpdateCount = stats.getEntityUpdateCount();
    entityDeleteCount = stats.getEntityDeleteCount();
    entityFetchCount = stats.getEntityFetchCount();
    queryCacheHitCount = stats.getQueryCacheHitCount();
    queryCacheMissCount = stats.getQueryCacheMissCount();
    queryCachePutCount = stats.getQueryCachePutCount();
    secondLevelCacheHitCount = stats.getSecondLevelCacheHitCount();
    secondLevelCacheMissCount = stats.getSecondLevelCacheMissCount();
    secondLevelCachePutCount = stats.getSecondLevelCachePutCount();
    stats.clear();
}

From source file:net.big_oh.common.utils.HibernateStatisticsUtil.java

License:Open Source License

/**
 * Gets the hit ratio for Hibernate's second level caches.
 * //from   w  w w.ja  v a2 s .c  om
 * @param hibernateStats
 * @return A value between 0 and 1 (inclusive) representing the cache hit
 *         ratio.
 * @throws IllegalArgumentException
 *             Thrown if the hibernateStats object is null, or does not have
 *             statistics enabled.
 */
// TODO Unit test me!
public static double getSecondLevelCacheHitRatio(Statistics hibernateStats) throws IllegalArgumentException {

    if (hibernateStats == null) {
        throw new IllegalArgumentException("The hibernateStats may not be null.");
    }
    if (!hibernateStats.isStatisticsEnabled()) {
        throw new IllegalArgumentException("Hibernate stats are unexpectedly not enabled!");
    }

    return (double) hibernateStats.getSecondLevelCacheHitCount() / (double) Math.max(1,
            (hibernateStats.getSecondLevelCacheHitCount() + hibernateStats.getSecondLevelCacheMissCount()));

}

From source file:net.welen.buzz.typehandler.impl.hibernate.HibernateStatisticsUnitHandler.java

License:Open Source License

public void getValues(BuzzAnswer values) throws TypeHandlerException {
    for (String name : getMeasurableUnits()) {
        boolean debug = LOG.isDebugEnabled();

        // Find the Hibernate Session Factory in JNDI
        SessionFactory sessionFactory = null;

        Object o;/*w  ww. j  a  v a2 s. c  o  m*/
        try {
            if (debug) {
                LOG.debug("Looking up: " + name);
            }
            o = new InitialContext().lookup(name);
        } catch (NamingException e) {
            throw new TypeHandlerException(e);
        }
        if (debug) {
            LOG.debug("Found class: " + o.getClass().getName());
        }

        if (o.getClass().getName().equals("org.hibernate.ejb.EntityManagerFactoryImpl")) {
            // Hibernate 4
            sessionFactory = ((EntityManagerFactoryImpl) o).getSessionFactory();
        } else {
            // Hibernate 3
            sessionFactory = (SessionFactory) o;
        }

        // Get all values
        Statistics stats = sessionFactory.getStatistics();
        String measurementUnit = getMeasurementUnit();
        values.put(measurementUnit, name, "SessionOpenCount", stats.getSessionOpenCount());
        values.put(measurementUnit, name, "SessionCloseCount", stats.getSessionCloseCount());
        values.put(measurementUnit, name, "FlushCount", stats.getFlushCount());
        values.put(measurementUnit, name, "ConnectCount", stats.getConnectCount());
        values.put(measurementUnit, name, "PrepareStatementCount", stats.getPrepareStatementCount());
        values.put(measurementUnit, name, "CloseStatementCount", stats.getCloseStatementCount());
        values.put(measurementUnit, name, "EntityLoadCount", stats.getEntityLoadCount());
        values.put(measurementUnit, name, "EntityUpdateCount", stats.getEntityUpdateCount());
        values.put(measurementUnit, name, "EntityInsertCount", stats.getEntityInsertCount());
        values.put(measurementUnit, name, "EntityDeleteCount", stats.getEntityDeleteCount());
        values.put(measurementUnit, name, "EntityFetchCount", stats.getEntityFetchCount());
        values.put(measurementUnit, name, "CollectionLoadCount", stats.getCollectionLoadCount());
        values.put(measurementUnit, name, "CollectionUpdateCount", stats.getCollectionUpdateCount());
        values.put(measurementUnit, name, "CollectionRemoveCount", stats.getCollectionRemoveCount());
        values.put(measurementUnit, name, "CollectionRecreateCount", stats.getCollectionRecreateCount());
        values.put(measurementUnit, name, "CollectionFetchCount", stats.getCollectionFetchCount());
        values.put(measurementUnit, name, "SecondLevelCacheHitCount", stats.getSecondLevelCacheHitCount());
        values.put(measurementUnit, name, "SecondLevelCacheMissCount", stats.getSecondLevelCacheMissCount());
        values.put(measurementUnit, name, "SecondLevelCachePutCount", stats.getSecondLevelCachePutCount());
        values.put(measurementUnit, name, "QueryExecutionCount", stats.getQueryExecutionCount());
        values.put(measurementUnit, name, "QueryExecutionMaxTime", stats.getQueryExecutionMaxTime());
        values.put(measurementUnit, name, "QueryCacheHitCount", stats.getQueryCacheHitCount());
        values.put(measurementUnit, name, "QueryCacheMissCount", stats.getQueryCacheMissCount());
        values.put(measurementUnit, name, "QueryCachePutCount", stats.getQueryCachePutCount());
        values.put(measurementUnit, name, "TransactionCount", stats.getTransactionCount());
        values.put(measurementUnit, name, "OptimisticFailureCount", stats.getOptimisticFailureCount());

        // TODO What about?
        // sessionFactory.getStatistics().getEntityStatistics(<parameter from setup? OR loop?>).
        // sessionFactory.getStatistics().getCollectionStatistics(<parameter from setup? OR loop?>)
        // sessionFactory.getStatistics().getQueryStatistics(<<parameter from setup? OR loop?>)
    }
}

From source file:org.cast.cwm.admin.CacheManagementPage.java

License:Open Source License

public CacheManagementPage(PageParameters parameters) {
    super(parameters);

    Statistics statistics = Databinder.getHibernateSessionFactory().getStatistics();

    add(new Label("statisticsEnabled", statistics.isStatisticsEnabled() ? "on" : "off"));

    long hitCount = statistics.getSecondLevelCacheHitCount();
    long missCount = statistics.getSecondLevelCacheMissCount();
    long totalCount = hitCount + missCount;

    add(new Label("slcPut", String.valueOf(statistics.getSecondLevelCachePutCount())));
    add(new Label("slcHit", String.valueOf(hitCount)));
    add(new Label("slcMiss", String.valueOf(missCount)));
    add(new Label("slcHitP", String.valueOf(totalCount > 0 ? Math.round(100d * hitCount / totalCount) : 0)));
    add(new Label("slcMissP", String.valueOf(totalCount > 0 ? Math.round(100d * missCount / totalCount) : 0)));

    CacheManager ehcache = CacheManager.getInstance();
    String[] caches = ehcache.getCacheNames();
    Arrays.sort(caches);//from   w w w  .  jav a2 s  .  com

    RepeatingView cacheView = new RepeatingView("cacheView");
    add(cacheView);
    for (String name : caches) {
        WebMarkupContainer eView = new WebMarkupContainer(cacheView.newChildId());
        cacheView.add(eView);
        net.sf.ehcache.Cache cache = ehcache.getCache(name);
        int size = cache.getSize();

        eView.add(new Label("name", name));
        eView.add(new Label("items", size > 0 ? String.valueOf(size) : ""));
    }

    add(new ClearCacheLink("clear"));

}