Example usage for org.hibernate.stat Statistics getSecondLevelCacheRegionNames

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

Introduction

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

Prototype

String[] getSecondLevelCacheRegionNames();

Source Link

Document

Get all second-level cache region names.

Usage

From source file:com.daphne.es.monitor.web.controller.HibernateCacheMonitorController.java

License:Apache License

private void setMemoryInfo(Model model) {
    ///*  w ww  . java  2 s.c  om*/
    MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    long usedSystemMemory = heapMemoryUsage.getUsed();
    long maxSystemMemory = heapMemoryUsage.getMax();
    model.addAttribute("usedSystemMemory", usedSystemMemory);
    model.addAttribute("maxSystemMemory", maxSystemMemory);

    //
    Statistics statistics = (Statistics) model.asMap().get("statistics");
    String[] secondLevelCacheRegionNames = statistics.getSecondLevelCacheRegionNames();

    int totalMemorySize = 0;
    int totalMemoryCount = 0;
    int totalDiskCount = 0;

    for (String secondLevelCacheRegionName : secondLevelCacheRegionNames) {
        SecondLevelCacheStatistics secondLevelCacheStatistics = statistics
                .getSecondLevelCacheStatistics(secondLevelCacheRegionName);
        totalMemorySize += secondLevelCacheStatistics.getSizeInMemory();
        totalMemoryCount += secondLevelCacheStatistics.getElementCountInMemory();
        totalDiskCount += secondLevelCacheStatistics.getElementCountOnDisk();
    }

    model.addAttribute("totalMemorySize", totalMemorySize);
    model.addAttribute("totalMemoryCount", totalMemoryCount);
    model.addAttribute("totalDiskCount", totalDiskCount);
}

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

License:Apache License

/**
 * Log the current statistics/*from ww w.  j  a  v a 2s. 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.openkm.servlet.admin.HibernateStatsServlet.java

License:Open Source License

/**
 * Refresh stats/*from  w  ww.  j  a  va  2 s.  co  m*/
 */
private synchronized void refresh() {
    Statistics statistics = HibernateUtil.getSessionFactory().getStatistics();
    generalStatistics.set(0, statistics.getConnectCount());
    generalStatistics.set(1, statistics.getFlushCount());
    generalStatistics.set(2, statistics.getPrepareStatementCount());
    generalStatistics.set(3, statistics.getCloseStatementCount());
    generalStatistics.set(4, statistics.getSessionCloseCount());
    generalStatistics.set(5, statistics.getSessionOpenCount());
    generalStatistics.set(6, statistics.getTransactionCount());
    generalStatistics.set(7, statistics.getSuccessfulTransactionCount());
    generalStatistics.set(8, statistics.getOptimisticFailureCount());
    queryStatistics.clear();
    String[] names = statistics.getQueries();

    if (names != null && names.length > 0) {
        for (int i = 0; i < names.length; i++) {
            queryStatistics.put(names[i], statistics.getQueryStatistics(names[i]));
        }
    }

    entityStatistics.clear();
    names = statistics.getEntityNames();

    if (names != null && names.length > 0) {
        for (int i = 0; i < names.length; i++) {
            entityStatistics.put(names[i], statistics.getEntityStatistics(names[i]));
        }
    }

    collectionStatistics.clear();
    names = statistics.getCollectionRoleNames();

    if (names != null && names.length > 0) {
        for (int i = 0; i < names.length; i++) {
            collectionStatistics.put(names[i], statistics.getCollectionStatistics(names[i]));
        }
    }

    secondLevelCacheStatistics.clear();
    names = statistics.getSecondLevelCacheRegionNames();

    if (names != null && names.length > 0) {
        for (int i = 0; i < names.length; i++) {
            secondLevelCacheStatistics.put(names[i], statistics.getSecondLevelCacheStatistics(names[i]));
        }
    }
}

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  w  w . j  av  a2  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 .ja  v a 2  s  .  c o m*/
    }

}

From source file:net.sf.ehcache.hibernate.management.impl.HibernateStatsImpl.java

License:Apache License

/**
 * {@inheritDoc}// w  w w .  j av a2 s  . c o  m
 */
public TabularData getCacheRegionStats() {
    List<CompositeData> list = new ArrayList<CompositeData>();
    Statistics statistics = getStatistics();
    for (String region : statistics.getSecondLevelCacheRegionNames()) {
        CacheRegionStats l2CacheStats = new CacheRegionStats(region,
                statistics.getSecondLevelCacheStatistics(region));
        list.add(l2CacheStats.toCompositeData());
    }
    TabularData td = CacheRegionStats.newTabularDataInstance();
    td.putAll(list.toArray(new CompositeData[list.size()]));
    return td;
}

From source file:org.bedework.calcore.hibernate.DbStatistics.java

License:Apache License

/** Get the current statistics
 *
 * @param dbStats//from w w  w .ja  va 2  s . c  om
 * @return Collection
 */
public static Collection<StatsEntry> getStats(Statistics dbStats) {
    /* XXX this ought to be property driven to some extent. The cache stats in
     * particular.
     */
    ArrayList<StatsEntry> al = new ArrayList<StatsEntry>();

    if (dbStats == null) {
        return al;
    }

    al.add(new StatsEntry("Database statistics"));

    al.add(new StatsEntry("Number of connection requests", dbStats.getConnectCount()));
    al.add(new StatsEntry("Session flushes", dbStats.getFlushCount()));
    al.add(new StatsEntry("Transactions", dbStats.getTransactionCount()));
    al.add(new StatsEntry("Successful transactions", dbStats.getSuccessfulTransactionCount()));
    al.add(new StatsEntry("Sessions opened", dbStats.getSessionOpenCount()));
    al.add(new StatsEntry("Sessions closed", dbStats.getSessionCloseCount()));
    al.add(new StatsEntry("Queries executed", dbStats.getQueryExecutionCount()));
    al.add(new StatsEntry("Max query time", dbStats.getQueryExecutionMaxTime()));
    al.add(new StatsEntry("Max time query", dbStats.getQueryExecutionMaxTimeQueryString()));

    al.add(new StatsEntry("Collection statistics"));

    al.add(new StatsEntry("Collections fetched", dbStats.getCollectionFetchCount()));
    al.add(new StatsEntry("Collections loaded", dbStats.getCollectionLoadCount()));
    al.add(new StatsEntry("Collections rebuilt", dbStats.getCollectionRecreateCount()));
    al.add(new StatsEntry("Collections batch deleted", dbStats.getCollectionRemoveCount()));
    al.add(new StatsEntry("Collections batch updated", dbStats.getCollectionUpdateCount()));

    al.add(new StatsEntry("Object statistics"));

    al.add(new StatsEntry("Objects fetched", dbStats.getEntityFetchCount()));
    al.add(new StatsEntry("Objects loaded", dbStats.getEntityLoadCount()));
    al.add(new StatsEntry("Objects inserted", dbStats.getEntityInsertCount()));
    al.add(new StatsEntry("Objects deleted", dbStats.getEntityDeleteCount()));
    al.add(new StatsEntry("Objects updated", dbStats.getEntityUpdateCount()));

    al.add(new StatsEntry("Cache statistics"));

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

    al.add(new StatsEntry("Cache hit count", chit));
    al.add(new StatsEntry("Cache miss count", cmiss));
    al.add(new StatsEntry("Cache hit ratio", chit / (chit + cmiss)));

    entityStats(al, dbStats, BwCalendar.class);
    entityStats(al, dbStats, BwEventObj.class);
    entityStats(al, dbStats, BwEventAnnotation.class);
    entityStats(al, dbStats, BwCategory.class);
    entityStats(al, dbStats, BwLocation.class);
    entityStats(al, dbStats, BwContact.class);
    entityStats(al, dbStats, BwUser.class);

    collectionStats(al, dbStats, BwCalendar.class, "children");

    collectionStats(al, dbStats, BwEventObj.class, "attendees");
    collectionStats(al, dbStats, BwEventObj.class, "categories");
    collectionStats(al, dbStats, BwEventObj.class, "descriptions");
    collectionStats(al, dbStats, BwEventObj.class, "summaries");

    collectionStats(al, dbStats, BwEventObj.class, "rrules");
    collectionStats(al, dbStats, BwEventObj.class, "rdates");
    collectionStats(al, dbStats, BwEventObj.class, "exdates");

    collectionStats(al, dbStats, BwEventAnnotation.class, "attendees");
    collectionStats(al, dbStats, BwEventAnnotation.class, "categories");
    collectionStats(al, dbStats, BwEventAnnotation.class, "descriptions");
    collectionStats(al, dbStats, BwEventAnnotation.class, "summaries");

    collectionStats(al, dbStats, BwEventAnnotation.class, "rrules");
    collectionStats(al, dbStats, BwEventAnnotation.class, "rdates");
    collectionStats(al, dbStats, BwEventAnnotation.class, "exdates");

    String[] qs = dbStats.getQueries();

    for (String q : qs) {
        queryStats(al, dbStats, q);
    }

    String[] slcrn = dbStats.getSecondLevelCacheRegionNames();

    for (String s : slcrn) {
        secondLevelStats(al, dbStats, s);
    }

    return al;
}

From source file:org.horizontaldb.shard.hibernate.AbstractDaoEnricher.java

License:Apache License

private void logSlcStats(Session session, String tenantId) {
    if (LOG.isTraceEnabled() && session != null) {
        Statistics statistics = session.getSessionFactory().getStatistics();

        if (statistics != null && statistics.isStatisticsEnabled()) {
            String[] regions = statistics.getSecondLevelCacheRegionNames();

            for (String region : regions) {
                SecondLevelCacheStatistics stat = statistics.getSecondLevelCacheStatistics(region);

                LOG.trace(String.format(
                        "secondLevelCacheStatistics.%s.%s=hits[%s], misses[%s], puts[%s], memCount[%s], memSize[%s], diskCount[%s]",
                        tenantId, region, stat.getHitCount(), stat.getMissCount(), stat.getPutCount(),
                        stat.getElementCountInMemory(), stat.getSizeInMemory(), stat.getElementCountOnDisk()));
            }/*from w w w  .  ja v a2 s.  c  o m*/
        }
    }
}

From source file:org.jboss.as.jpa.hibernate4.management.HibernateStatisticsResource.java

License:Open Source License

private Set<String> getCacheRegionNames() {
    final Statistics stats = getStatistics();
    if (stats == null) {
        return Collections.emptySet();
    } else {//from ww  w  .  j av  a 2s.c  o m
        Set<String> result = new HashSet<String>();
        String[] cacheRegionNames = stats.getSecondLevelCacheRegionNames();
        if (cacheRegionNames != null) {
            for (String region : cacheRegionNames) {

                // example regionName = "jpa_SecondLevelCacheTestCase.jar#mypc.org.jboss.as.test.integration.jpa.hibernate.Employee"
                // remove the scoped PU name plus one for '.' the separator character added to it.
                // and replace period with underscore.  Filtered region name will be "org_jboss_as_testsuite_integration_jpa_hibernate_Employee"
                int stripUpTo = puName.length() + 1;
                result.add(region.substring(stripUpTo));
            }
        }
        return result;
    }
}

From source file:org.jboss.as.test.integration.jpa.secondlevelcache.SFSB2LC.java

License:Open Source License

/**
 * Check if disabling 2LC works as expected
 *//*from w w  w . ja  v  a  2s  .c  o  m*/
public String disabled2LCCheck() {

    EntityManager em = emfNo2LC.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();

    try {
        // check if entities are NOT cached in 2LC
        String[] names = stats.getSecondLevelCacheRegionNames();
        assertEquals("There aren't any 2LC regions.", 0, names.length);

        createEmployee(em, "Martin", "Prague 132", 1);
        assertEquals("There aren't any puts in the 2LC.", 0, stats.getSecondLevelCachePutCount());

        // check if queries are NOT cached in 2LC
        Employee emp = getEmployeeQuery(em, 1);
        assertNotNull("Employee returned", emp);
        assertEquals("There aren't any query puts in the 2LC.", 0, stats.getQueryCachePutCount());

        // cleanup
        em.remove(emp);

    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}