Example usage for org.hibernate.stat Statistics isStatisticsEnabled

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

Introduction

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

Prototype

boolean isStatisticsEnabled();

Source Link

Document

Are statistics enabled

Usage

From source file:com.abiquo.server.core.common.persistence.HibernateSessionAllocationChecker.java

License:Open Source License

public HibernateSessionAllocationChecker(EntityManagerFactory factory) {
    assert factory != null;

    this.factory = factory;

    EntityManager em = factory.createEntityManager();
    try {/*from   ww  w .  ja va2 s . c  o  m*/
        Session session = (Session) em.getDelegate();
        Statistics statistics = session.getSessionFactory().getStatistics();
        this.statisticsEnabledOnEntry = statistics.isStatisticsEnabled();
        statistics.setStatisticsEnabled(true);
        this.outstandingSessionsOnEntry = statistics.getSessionOpenCount() - statistics.getSessionCloseCount();
        try {
            this.openMySqlConnectionsOnEntry = getMySqlConnectionCount(session.connection());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    } finally {
        em.close();
    }
}

From source file:com.abiquo.server.core.common.persistence.HibernateSessionAllocationChecker.java

License:Open Source License

private void ensureResultObtained() {
    if (!resultObtained) {
        EntityManager em = factory.createEntityManager();
        try {/*from w w w . jav  a  2  s .  c  o m*/
            Session session = (Session) em.getDelegate();
            Statistics statistics = session.getSessionFactory().getStatistics();

            assert statistics
                    .isStatisticsEnabled() : "Somebody else disabled the statistics: DON'T do, or do not use this component";

            long outstandingSessionsOnExit = statistics.getSessionOpenCount()
                    - statistics.getSessionCloseCount();
            this.sessionsNotReleasedAfterCreation = outstandingSessionsOnExit - this.outstandingSessionsOnEntry;

            assert sessionsNotReleasedAfterCreation >= 0 : "Somebody else played with the statistics (maybe cleared/disabled them?): DON'T do, or do not use this component";
            // System.err.println( "OUTSTANDING SESSIONS: " +
            // sessionsNotReleasedAfterCreation);

            try {
                this.mySqlConnectionsNoReleasedAfterCreation = getMySqlConnectionCount(session.connection())
                        - this.openMySqlConnectionsOnEntry;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            // Reset statistics to the state they were before we started to
            // be interested in open/closed session count
            statistics.setStatisticsEnabled(this.statisticsEnabledOnEntry);
        } finally {
            em.close();
        }
        resultObtained = true;
    }

}

From source file:com.medigy.persist.util.HibernateUtil.java

License:Open Source License

public static void logStatistics() {

    Statistics stats = HibernateUtil.sessionFactory.getStatistics();
    if (!stats.isStatisticsEnabled()) {
        log.warn("Statistics are not enabled.");
        return;//from   www  . j  a v a2s.com
    }
    log.info("Connection count: " + stats.getConnectCount());
    log.info("Session open count: " + stats.getSessionOpenCount());
    log.info("Session close count: " + stats.getSessionCloseCount());

    log.info("Entities insert count: " + stats.getEntityInsertCount());
    log.info("Entities update count: " + stats.getEntityUpdateCount());
    log.info("Entities fetch count: " + stats.getEntityFetchCount());

    log.info("Collection fetch count: " + stats.getCollectionFetchCount());
    log.info("Collection load count: " + stats.getCollectionLoadCount());
    double queryCacheHitCount = stats.getQueryCacheHitCount();
    double queryCacheMissCount = stats.getQueryCacheMissCount();
    double queryCacheHitRatio = queryCacheHitCount / (queryCacheHitCount + queryCacheMissCount);
    log.info("Query Hit ratio:" + queryCacheHitRatio);
}

From source file:com.openkm.servlet.admin.HibernateStatsServlet.java

License:Open Source License

/**
 * Activate stats//w  w w.  j a  v a  2  s  . c  o  m
 */
private void activate(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    Statistics stats = HibernateUtil.getSessionFactory().getStatistics();

    if (!stats.isStatisticsEnabled()) {
        stats.setStatisticsEnabled(true);
    }
}

From source file:com.openkm.servlet.admin.HibernateStatsServlet.java

License:Open Source License

/**
 * Deactivate stats/*  w  w  w .j av  a  2  s. c om*/
 */
private void deactivate(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    Statistics stats = HibernateUtil.getSessionFactory().getStatistics();

    if (stats.isStatisticsEnabled()) {
        stats.setStatisticsEnabled(false);
    }
}

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;
    }/*from   w  ww.ja  v a2s  .c o  m*/
    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:com.zutubi.pulse.master.xwork.actions.admin.debug.HibernateStatisticsAction.java

License:Apache License

public String toggle() throws Exception {
    Statistics stats = sessionFactory.getStatistics();
    stats.setStatisticsEnabled(!stats.isStatisticsEnabled());

    return "toggled";
}

From source file:io.micrometer.core.instrument.binder.jpa.HibernateMetrics.java

License:Apache License

private boolean hasStatisticsEnabled(EntityManagerFactory emf) {
    final Statistics stats = getStatistics(emf);
    return (stats != null && stats.isStatisticsEnabled());
}

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

License:Open Source License

/**
 * Gets the hit ratio for Hibernate's second level caches.
 * /* w  w  w .j  a  va2s  .  c o m*/
 * @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.big_oh.common.utils.HibernateStatisticsUtil.java

License:Open Source License

/**
 * Gets the hit ratio for Hibernate's query cache.
 * /* ww w .  j  a  v  a2  s .  c o  m*/
 * @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 getQueryCacheHitRatio(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.getQueryCacheHitCount() / (double) Math.max(1,
            (hibernateStats.getQueryCacheHitCount() + hibernateStats.getQueryCacheMissCount()));

}