Example usage for org.hibernate.stat Statistics getNaturalIdCacheHitCount

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

Introduction

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

Prototype

long getNaturalIdCacheHitCount();

Source Link

Document

Get the global number of cached natural id lookups successfully retrieved from cache

Usage

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 ava2s .com
    }

}

From source file:org.infinispan.test.hibernate.cache.commons.functional.ReadWriteTest.java

License:LGPL

@Test
public void testNaturalIdCached() throws Exception {
    saveSomeCitizens();/*from   w w w  . j  a  v  a 2  s.c om*/

    // Clear the cache before the transaction begins
    cleanupCache();
    TIME_SERVICE.advance(1);

    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);

        Statistics stats = sessionFactory().getStatistics();
        stats.setStatisticsEnabled(true);
        stats.clear();
        assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());

        // first query
        List results = criteria.list();
        assertEquals(1, results.size());
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());

        // query a second time - result should be cached in session
        criteria.list();
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
    });
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.ReadWriteTest.java

License:LGPL

@Test
public void testNaturalIdLoaderCached() throws Exception {
    final Statistics stats = sessionFactory().getStatistics();
    stats.setStatisticsEnabled(true);/*from ww w .j a v  a2  s  .  c o m*/
    stats.clear();

    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

    saveSomeCitizens();

    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

    //Try NaturalIdLoadAccess after insert
    final Citizen citizen = withTxSessionApply(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess<Citizen> naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);

        //Not clearing naturalId caches, should be warm from entity loading
        stats.clear();

        // first query
        Citizen c = naturalIdLoader.load();
        assertNotNull(c);
        assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
        return c;
    });

    // TODO: Clear caches manually via cache manager (it's faster!!)
    cleanupCache();
    TIME_SERVICE.advance(1);
    stats.setStatisticsEnabled(true);
    stats.clear();

    //Try NaturalIdLoadAccess
    withTxSession(s -> {
        // first query
        Citizen loadedCitizen = (Citizen) s.get(Citizen.class, citizen.getId());
        assertNotNull(loadedCitizen);
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
    });

    // Try NaturalIdLoadAccess after load
    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);

        //Not clearing naturalId caches, should be warm from entity loading
        stats.setStatisticsEnabled(true);
        stats.clear();

        // first query
        Citizen loadedCitizen = (Citizen) naturalIdLoader.load();
        assertNotNull(loadedCitizen);
        assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
    });

}