Example usage for org.hibernate.stat Statistics getQueryExecutionCount

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

Introduction

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

Prototype

long getQueryExecutionCount();

Source Link

Document

Get global number of executed queries

Usage

From source file:com.francetelecom.clara.cloud.commons.HibernateStatsHelper.java

License:Apache License

/**
 * Ensure no regression higher than 5%//from w w w  .  j a v  a 2  s.  co  m
 * @param assertDuration if true durations are verified, if false durations are not verified
 */
private static void checkStats(Map<HibernateStatsReferenceType, Long> refs, long duration, Statistics stats,
        boolean assertDuration) throws ObjectNotFoundException, MalformedURLException {
    List<AssertionError> failedAsserts = new ArrayList<AssertionError>();

    // Reference values: these must be updated when you optimize your code
    // or if new values are explained and normal.
    final long DURATION = refs.get(HibernateStatsReferenceType.DURATION);
    final int QUERY_COUNT = refs.get(HibernateStatsReferenceType.QUERY_COUNT).intValue();
    final int QUERY_MAX_TIME_MS = refs.get(HibernateStatsReferenceType.QUERY_MAX_TIME_MS).intValue();

    final int ENTITY_FETCH_COUNT = refs.get(HibernateStatsReferenceType.ENTITY_FETCH_COUNT).intValue();
    final int ENTITY_LOAD_COUNT = refs.get(HibernateStatsReferenceType.ENTITY_LOAD_COUNT).intValue();
    final int ENTITY_INSERT_COUNT = refs.get(HibernateStatsReferenceType.ENTITY_INSERT_COUNT).intValue();
    final int ENTITY_DELETE_COUNT = refs.get(HibernateStatsReferenceType.ENTITY_DELETE_COUNT).intValue();
    final int ENTITY_UPDATE_COUNT = refs.get(HibernateStatsReferenceType.ENTITY_UPDATE_COUNT).intValue();

    final int COLLECTION_FETCH_COUNT = refs.get(HibernateStatsReferenceType.COLLECTION_FETCH_COUNT).intValue();
    final int COLLECTION_LOAD_COUNT = refs.get(HibernateStatsReferenceType.COLLECTION_LOAD_COUNT).intValue();
    final int COLLECTION_RECREATE_COUNT = refs.get(HibernateStatsReferenceType.COLLECTION_RECREATE_COUNT)
            .intValue();
    final int COLLECTION_REMOVE_COUNT = refs.get(HibernateStatsReferenceType.COLLECTION_REMOVE_COUNT)
            .intValue();
    final int COLLECTION_UPDATE_COUNT = refs.get(HibernateStatsReferenceType.COLLECTION_UPDATE_COUNT)
            .intValue();

    // The number of completed transactions (failed and successful) must
    // match number of transactions completed without failure
    preAssertEquals("There are transaction failures", stats.getTransactionCount(),
            stats.getSuccessfulTransactionCount(), failedAsserts);

    // Total number of queries executed.
    preAssertTrue(
            "Total number of queries executed increased more than 5% (ref=" + QUERY_COUNT + "): "
                    + stats.getQueryExecutionCount(),
            stats.getQueryExecutionCount() <= (QUERY_COUNT * 1.05), failedAsserts);
    if (stats.getQueryExecutionCount() < (QUERY_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value QUERY_COUNT (ref=" + QUERY_COUNT + ") to "
                + stats.getQueryExecutionCount());

    preAssertTrue(
            "ENTITY_DELETE_COUNT increased more than 5% (ref=" + ENTITY_DELETE_COUNT + "): "
                    + stats.getEntityDeleteCount(),
            stats.getEntityDeleteCount() <= (ENTITY_DELETE_COUNT * 1.05), failedAsserts);
    if (stats.getEntityDeleteCount() < (ENTITY_DELETE_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value ENTITY_DELETE_COUNT (ref=" + ENTITY_DELETE_COUNT
                + ") to " + stats.getEntityDeleteCount());
    preAssertTrue(
            "ENTITY_UPDATE_COUNT increased more than 5% (ref=" + ENTITY_UPDATE_COUNT + "): "
                    + stats.getEntityUpdateCount(),
            stats.getEntityUpdateCount() <= (ENTITY_UPDATE_COUNT * 1.05), failedAsserts);
    if (stats.getEntityUpdateCount() < (ENTITY_UPDATE_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value ENTITY_UPDATE_COUNT (ref=" + ENTITY_UPDATE_COUNT
                + ") to " + stats.getEntityUpdateCount());

    if (stats.getCollectionRecreateCount() < (COLLECTION_RECREATE_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value COLLECTION_RECREATE_COUNT (ref="
                + COLLECTION_RECREATE_COUNT + ") to " + stats.getCollectionRecreateCount());
    preAssertTrue(
            "COLLECTION_REMOVE_COUNT increased more than 5% (ref=" + COLLECTION_REMOVE_COUNT + "): "
                    + stats.getCollectionRemoveCount(),
            stats.getCollectionRemoveCount() <= (COLLECTION_REMOVE_COUNT * 1.05), failedAsserts);
    if (stats.getCollectionRemoveCount() < (COLLECTION_REMOVE_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value COLLECTION_REMOVE_COUNT (ref="
                + COLLECTION_REMOVE_COUNT + ") to " + stats.getCollectionRemoveCount());
    preAssertTrue(
            "COLLECTION_UPDATE_COUNT increased more than 5% (ref=" + COLLECTION_UPDATE_COUNT + "): "
                    + stats.getCollectionUpdateCount(),
            stats.getCollectionUpdateCount() <= (COLLECTION_UPDATE_COUNT * 1.05), failedAsserts);
    if (stats.getCollectionUpdateCount() < (COLLECTION_UPDATE_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value COLLECTION_UPDATE_COUNT (ref="
                + COLLECTION_UPDATE_COUNT + ") to " + stats.getCollectionUpdateCount());

    // Entities statistics
    preAssertTrue(
            "ENTITY_FETCH_COUNT increased more than 5% (ref=" + ENTITY_FETCH_COUNT + "): "
                    + stats.getEntityFetchCount(),
            stats.getEntityFetchCount() < (ENTITY_FETCH_COUNT * 1.05), failedAsserts);
    if (stats.getEntityFetchCount() < (ENTITY_FETCH_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value ENTITY_FETCH_COUNT (ref=" + ENTITY_FETCH_COUNT
                + ") to " + stats.getEntityFetchCount());
    preAssertTrue(
            "ENTITY_LOAD_COUNT increased more than 5% (ref=" + ENTITY_LOAD_COUNT + "): "
                    + stats.getEntityLoadCount(),
            stats.getEntityLoadCount() <= (ENTITY_LOAD_COUNT * 1.05), failedAsserts);
    if (stats.getEntityLoadCount() < (ENTITY_LOAD_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value ENTITY_LOAD_COUNT (ref=" + ENTITY_LOAD_COUNT
                + ") to " + stats.getEntityLoadCount());
    preAssertTrue(
            "ENTITY_INSERT_COUNT increased more than 5% (ref=" + ENTITY_INSERT_COUNT + "): "
                    + stats.getEntityInsertCount(),
            stats.getEntityInsertCount() <= (ENTITY_INSERT_COUNT * 1.05), failedAsserts);
    if (stats.getEntityInsertCount() < (ENTITY_INSERT_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value ENTITY_INSERT_COUNT (ref=" + ENTITY_INSERT_COUNT
                + ") to " + stats.getEntityInsertCount());

    // Collections statistics
    preAssertTrue(
            "COLLECTION_FETCH_COUNT increased more than 5% (ref=" + COLLECTION_FETCH_COUNT + "): "
                    + stats.getCollectionFetchCount(),
            stats.getCollectionFetchCount() <= (COLLECTION_FETCH_COUNT * 1.05), failedAsserts);
    if (stats.getCollectionFetchCount() < (COLLECTION_FETCH_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value COLLECTION_FETCH_COUNT (ref="
                + COLLECTION_FETCH_COUNT + ") to " + stats.getCollectionFetchCount());
    preAssertTrue(
            "COLLECTION_LOAD_COUNT increased more than 5% (ref=" + COLLECTION_LOAD_COUNT + "): "
                    + stats.getCollectionLoadCount(),
            stats.getCollectionLoadCount() <= (COLLECTION_LOAD_COUNT * 1.05), failedAsserts);
    if (stats.getCollectionLoadCount() < (COLLECTION_LOAD_COUNT * 0.95))
        logger.warn("/!\\ You should update reference value COLLECTION_LOAD_COUNT (ref=" + COLLECTION_LOAD_COUNT
                + ") to " + stats.getCollectionLoadCount());
    preAssertTrue(
            "COLLECTION_RECREATE_COUNT increased more than 5% (ref=" + COLLECTION_RECREATE_COUNT + "): "
                    + stats.getCollectionRecreateCount(),
            stats.getCollectionRecreateCount() <= (COLLECTION_RECREATE_COUNT * 1.05), failedAsserts);

    if (assertDuration) {
        // Time of the slowest query executed.
        preAssertTrue(
                "Time of the slowest query executed increased more than 50% (ref=" + QUERY_MAX_TIME_MS + "): "
                        + stats.getQueryExecutionMaxTime(),
                stats.getQueryExecutionMaxTime() <= (QUERY_MAX_TIME_MS * 1.50), failedAsserts);
        if (stats.getQueryExecutionMaxTime() < (QUERY_MAX_TIME_MS * 0.50))
            logger.warn("/!\\ You should update reference value QUERY_MAX_TIME_MS (ref=" + QUERY_MAX_TIME_MS
                    + ") to " + stats.getQueryExecutionMaxTime());

        // Check test duration
        preAssertTrue("Total duration of the test increased more than 5% (ref=" + DURATION + "): " + duration,
                duration < (DURATION * 1.05), failedAsserts);
        if (duration <= (DURATION * 0.85))
            logger.warn(
                    "/!\\ You should update reference value DURATION (ref=" + DURATION + ") to " + duration);
    }

    StringBuffer formattedFailedAsserts = new StringBuffer();
    for (AssertionError failedAssert : failedAsserts) {
        formattedFailedAsserts.append(failedAssert.getMessage());
        formattedFailedAsserts.append("\n");
    }
    String advice = "Analyse the code with your favorite profiler, then see where performances decrease and optimize the code. If you consider this new value as 'normal' then set a new reference value.";

    assertTrue(failedAsserts.size() + " Hibernate stats violations: \n" + formattedFailedAsserts.toString()
            + advice, failedAsserts.isEmpty());
}

From source file:com.francetelecom.clara.cloud.scalability.helper.StatisticsHelper.java

License:Apache License

/**
 * Log the current statistics//from w  w  w . ja  v a 2 s .  c  o  m
 *
 * @param stats hibernate statistics
 */
public static void logStats(Statistics stats) {

    logger.info("Database statistics");

    logger.info("  Number of connection requests : " + stats.getConnectCount());
    logger.info("  Session flushes : " + stats.getFlushCount());
    logger.info("  Transactions : " + stats.getTransactionCount());
    logger.info("  Successful transactions : " + stats.getSuccessfulTransactionCount());
    logger.info("  Sessions opened : " + stats.getSessionOpenCount());
    logger.info("  Sessions closed : " + stats.getSessionCloseCount());
    logger.info("  Queries executed : " + stats.getQueryExecutionCount());
    logger.info("  Max query time : " + stats.getQueryExecutionMaxTime());
    logger.info("  Max time query : " + stats.getQueryExecutionMaxTimeQueryString());

    logger.info("Collection statistics");

    logger.info("  Collections fetched : " + stats.getCollectionFetchCount());
    logger.info("  Collections loaded : " + stats.getCollectionLoadCount());
    logger.info("  Collections rebuilt : " + stats.getCollectionRecreateCount());
    logger.info("  Collections batch deleted : " + stats.getCollectionRemoveCount());
    logger.info("  Collections batch updated : " + stats.getCollectionUpdateCount());

    logger.info("Object statistics");

    logger.info("  Objects fetched : " + stats.getEntityFetchCount());
    logger.info("  Objects loaded : " + stats.getEntityLoadCount());
    logger.info("  Objects inserted : " + stats.getEntityInsertCount());
    logger.info("  Objects deleted : " + stats.getEntityDeleteCount());
    logger.info("  Objects updated : " + stats.getEntityUpdateCount());

    logger.info("Cache statistics");

    double chit = stats.getQueryCacheHitCount();
    double cmiss = stats.getQueryCacheMissCount();

    logger.info("  Cache hit count : " + chit);
    logger.info("  Cache miss count : " + cmiss);
    logger.info("  Cache hit ratio : " + (chit / (chit + cmiss)));

    String[] entityNames = stats.getEntityNames();
    Arrays.sort(entityNames);
    for (String entityName : entityNames) {
        Class<?> entityClass = null;
        try {
            entityClass = Class.forName(entityName);
        } catch (ClassNotFoundException e) {
            logger.error("Unable to load class for " + entityName, e);
        }
        entityStats(stats, entityClass);
    }
    //Uncomment these lines to trace every query (can generate a lot of logs)
    String[] qs = stats.getQueries();
    for (String q : qs) {
        queryStats(stats, q);
    }

    String[] slcrn = stats.getSecondLevelCacheRegionNames();
    for (String s : slcrn) {
        secondLevelStats(stats, s);
    }
}

From source file:com.francetelecom.clara.cloud.scalability.impl.ManageStatisticsImpl.java

License:Apache License

private Map<String, Long> getStatsValues(Collection<Class> entities) {
    Statistics stats = getStats();
    Map<String, Long> statistics = new HashMap<String, Long>();
    statistics.put("Number of connection requests", stats.getConnectCount());
    statistics.put("Sessions opened", stats.getSessionOpenCount());
    // statistics.put("Sessions closed", stats.getSessionCloseCount());
    statistics.put("Transactions", stats.getTransactionCount());
    // statistics.put("Successful transactions", stats.getSuccessfulTransactionCount());
    // statistics.put("Successful transactions", stats.getSuccessfulTransactionCount());
    statistics.put("Queries executed", stats.getQueryExecutionCount());
    for (Class entity : entities) {
        EntityStatistics eStats = stats.getEntityStatistics(entity.getName());
        statistics.put(entity.getSimpleName() + " Fetched", eStats.getFetchCount());
        statistics.put(entity.getSimpleName() + " Loaded", eStats.getLoadCount());
        statistics.put(entity.getSimpleName() + " Inserted", eStats.getInsertCount());
        statistics.put(entity.getSimpleName() + " Deleted", eStats.getDeleteCount());
        statistics.put(entity.getSimpleName() + " Updated", eStats.getUpdateCount());
    }/*w ww . j  a  va  2 s.c  o  m*/
    return statistics;
}

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);/*from  w w w  .j  ava 2 s . c  o  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 va2s. 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:dao.proyectoHelper.java

public static void reportConeciones() {
    Statistics stats = HibernateUtil.getSessionFactory().getStatistics();
    stats.setStatisticsEnabled(true);/* www  .  j  av  a  2s.c  o  m*/

    System.out.println("recuento de conexin: " + stats.getConnectCount());

    //Numero de transacciones completadas (falladas y satisfactorias)
    System.out.println("recuento Trx: " + stats.getTransactionCount());

    //Numero de transacciones completadas (solo satisfactorias)
    System.out.println("Succ trx count: " + stats.getSuccessfulTransactionCount());

    // Numero de sesiones que el codigo ha abierto
    System.out.println("sesiones abiertas: " + stats.getSessionOpenCount());

    // Numero de sesiones que el codigo ha cerrado
    System.out.println("sesiones  cerradas: " + stats.getSessionCloseCount());

    // Numero total de queries ejecutados
    System.out.println("No. queries: " + stats.getQueryExecutionCount());
    //            esta();
}

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  ww  w  .  ja v a 2 s  .c  o m
    }

}

From source file:es.ua.datos.cad.CAD.java

public void writeStatisticsLog(Statistics stats) {

    // Number of connection requests. Note that this number represents 
    // the number of times Hibernate asked for a connection, and 
    // NOT the number of connections (which is determined by your 
    // pooling mechanism).
    Date date = new Date();
    System.out.println("HORA: " + date);
    System.out.println("getConnectCount(): " + stats.getConnectCount());
    // Number of flushes done on the session (either by client code or 
    // by hibernate).
    System.out.println("getFlushCount(): " + stats.getFlushCount());
    // The number of completed transactions (failed and successful).
    System.out.println("getTransactionCount(): " + stats.getTransactionCount());
    // The number of transactions completed without failure
    System.out.println("getSuccessfulTransactionCount(): " + stats.getSuccessfulTransactionCount());
    // The number of sessions your code has opened.
    System.out.println("getSessionOpenCount(): " + stats.getSessionOpenCount());
    // The number of sessions your code has closed.
    System.out.println("getSessionCloseCount(): " + stats.getSessionCloseCount());
    // All of the queries that have executed.
    System.out.println("getQueries(): " + stats.getQueries());
    // Total number of queries executed.
    System.out.println("getQueryExecutionCount(): " + stats.getQueryExecutionCount());
    // Time of the slowest query executed.
    System.out.println("getQueryExecutionMaxTime(): " + stats.getQueryExecutionMaxTime());
}

From source file:models.papmon.HibernateStat.java

License:Open Source License

public HibernateStat(Date refDate) {
    created = refDate;/*from w  w w .  j av  a  2 s  . com*/
    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.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  w w  .ja v  a 2 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?>)
    }
}