Search in sources :

Example 1 with StatisticsImplementor

use of org.hibernate.stat.spi.StatisticsImplementor in project hibernate-orm by hibernate.

the class InheritedCacheableTest method testOnlySubclassIsCached.

@Test
public void testOnlySubclassIsCached() {
    final StatisticsImplementor statistics = sessionFactory().getStatistics();
    inTransaction(s -> {
        s.persist(new Employee("1", "John Doe", "987", "engineering"));
        s.persist(new Customer("2", "Acme Corp", "123"));
    });
    assertTrue(sessionFactory().getCache().contains(Employee.class, "1"));
    assertTrue(sessionFactory().getCache().contains(Person.class, "1"));
    assertFalse(sessionFactory().getCache().contains(Customer.class, "2"));
    assertFalse(sessionFactory().getCache().contains(Person.class, "2"));
    inTransaction(s -> {
        statistics.clear();
        final Customer customer = s.get(Customer.class, "2");
        assertTrue(Hibernate.isInitialized(customer));
        assertThat(statistics.getSecondLevelCacheHitCount(), CoreMatchers.is(0L));
        statistics.clear();
        final Employee emp = s.get(Employee.class, "1");
        assertTrue(Hibernate.isInitialized(emp));
        assertThat(statistics.getSecondLevelCacheHitCount(), CoreMatchers.is(1L));
    });
}
Also used : StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) Test(org.junit.Test)

Example 2 with StatisticsImplementor

use of org.hibernate.stat.spi.StatisticsImplementor in project hibernate-orm by hibernate.

the class SubclassOnlyCachingTests method testOnlySubclassIsCached.

@Test
public void testOnlySubclassIsCached() {
    final StatisticsImplementor statistics = sessionFactory().getStatistics();
    inTransaction(s -> s.persist(new Customer(1, "Acme Corp", "123")));
    assertTrue(sessionFactory().getCache().contains(Customer.class, 1));
    inTransaction(s -> {
        statistics.clear();
        final Customer customer = s.get(Customer.class, 1);
        assertTrue(Hibernate.isInitialized(customer));
        assertThat(statistics.getSecondLevelCacheHitCount(), CoreMatchers.is(1L));
    });
}
Also used : StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) Test(org.junit.Test)

Example 3 with StatisticsImplementor

use of org.hibernate.stat.spi.StatisticsImplementor in project hibernate-orm by hibernate.

the class StatisticsInitiator method initiateServiceInternal.

private StatisticsImplementor initiateServiceInternal(SessionFactoryImplementor sessionFactory, Object configValue, ServiceRegistryImplementor registry) {
    StatisticsFactory statisticsFactory;
    if (configValue == null) {
        statisticsFactory = DEFAULT_STATS_BUILDER;
    } else if (StatisticsFactory.class.isInstance(configValue)) {
        statisticsFactory = (StatisticsFactory) configValue;
    } else {
        // assume it names the factory class
        final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);
        try {
            statisticsFactory = (StatisticsFactory) classLoaderService.classForName(configValue.toString()).newInstance();
        } catch (HibernateException e) {
            throw e;
        } catch (Exception e) {
            throw new HibernateException("Unable to instantiate specified StatisticsFactory implementation [" + configValue.toString() + "]", e);
        }
    }
    StatisticsImplementor statistics = statisticsFactory.buildStatistics(sessionFactory);
    final boolean enabled = sessionFactory.getSettings().isStatisticsEnabled();
    statistics.setStatisticsEnabled(enabled);
    LOG.debugf("Statistics initialized [enabled=%s]", enabled);
    return statistics;
}
Also used : HibernateException(org.hibernate.HibernateException) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) StatisticsFactory(org.hibernate.stat.spi.StatisticsFactory) HibernateException(org.hibernate.HibernateException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 4 with StatisticsImplementor

use of org.hibernate.stat.spi.StatisticsImplementor in project hibernate-orm by hibernate.

the class RegionNameTest method testLegacyStatsSpi.

// todo (5.3) : any other API I can think of that deals with region-name?
// todo (6.0) : same ^^, maintain API compatibility
@Test
public void testLegacyStatsSpi() {
    // NOTE : these calls actually did change - the ability to provide
    // some better stats to the user
    // // these need to be the prefixed name
    // final String regionName = cachePrefix + '.' + localName;
    final String regionName = localName;
    final StatisticsImplementor statistics = sessionFactory().getStatistics();
    statistics.clear();
    statistics.naturalIdCacheHit(personNameRole, regionName);
    statistics.naturalIdCacheMiss(personNameRole, regionName);
    statistics.naturalIdCachePut(personNameRole, regionName);
    statistics.entityCacheHit(personRole, regionName);
    statistics.entityCacheMiss(personRole, regionName);
    statistics.entityCachePut(personRole, regionName);
    statistics.collectionCacheHit(personNickNamesRole, regionName);
    statistics.collectionCacheMiss(personNickNamesRole, regionName);
    statistics.collectionCachePut(personNickNamesRole, regionName);
    statistics.getNaturalIdCacheStatistics(cachePrefix + regionName);
    // stats for queries cannot be accessed second level cache regions map
    final String queryString = "select p from Person p";
    final String queryCacheRegionName = "x.y.z";
    final String prefixedQueryCacheRegionName = cachePrefix + '.' + queryCacheRegionName;
    inTransaction(// Only way to generate query region (to be accessible via stats) is to execute the query
    session -> session.createQuery(queryString).setCacheable(true).setCacheRegion(queryCacheRegionName).list());
    final SecondLevelCacheStatistics queryCacheStats = statistics.getSecondLevelCacheStatistics(prefixedQueryCacheRegionName);
    assert queryCacheStats != null;
    // note that
    statistics.queryCacheHit(queryString, prefixedQueryCacheRegionName);
    statistics.queryCacheMiss(queryString, prefixedQueryCacheRegionName);
    statistics.queryCachePut(queryString, prefixedQueryCacheRegionName);
// sessionFactory().getCache().evictQueryRegions();
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) Test(org.junit.Test)

Example 5 with StatisticsImplementor

use of org.hibernate.stat.spi.StatisticsImplementor in project hibernate-orm by hibernate.

the class QueryCacheTest method testQueryCacheInvalidation.

@Test
public void testQueryCacheInvalidation() throws Exception {
    sessionFactory().getCache().evictQueryRegions();
    final StatisticsImplementor statistics = sessionFactory().getStatistics();
    statistics.clear();
    final String queryString = "from Item i where i.name='widget'";
    final QueryStatistics qs = statistics.getQueryStatistics(queryString);
    final EntityStatistics es = statistics.getEntityStatistics(Item.class.getName());
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.createQuery(queryString).setCacheable(true).list();
    Item i = new Item();
    i.setName("widget");
    i.setDescription("A really top-quality, full-featured widget.");
    s.save(i);
    t.commit();
    s.close();
    // hit -> 0
    // miss -> 1
    // put -> 1
    assertEquals(es.getInsertCount(), 1);
    assertEquals(es.getUpdateCount(), 0);
    assertEquals(statistics.getQueryCacheHitCount(), 0);
    assertEquals(qs.getCacheHitCount(), 0);
    assertEquals(statistics.getQueryCacheMissCount(), 1);
    assertEquals(qs.getCacheMissCount(), 1);
    assertEquals(statistics.getQueryCachePutCount(), 1);
    assertEquals(qs.getCachePutCount(), 1);
    assertEquals(statistics.getQueryExecutionCount(), 1);
    assertEquals(qs.getExecutionCount(), 1);
    assertEquals(statistics.getEntityFetchCount(), 0);
    Thread.sleep(200);
    s = openSession();
    t = s.beginTransaction();
    List result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    t.commit();
    s.close();
    // hit -> 0
    // miss -> 2
    // put -> 2
    assertEquals(es.getInsertCount(), 1);
    assertEquals(es.getUpdateCount(), 0);
    assertEquals(statistics.getQueryCacheHitCount(), 0);
    assertEquals(qs.getCacheHitCount(), 0);
    assertEquals(statistics.getQueryCacheMissCount(), 2);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(statistics.getQueryCachePutCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    assertEquals(statistics.getQueryExecutionCount(), 2);
    assertEquals(qs.getExecutionCount(), 2);
    assertEquals(statistics.getEntityFetchCount(), 0);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    t.commit();
    s.close();
    // hit -> 1
    // miss -> 2
    // put -> 2
    assertEquals(es.getInsertCount(), 1);
    assertEquals(es.getUpdateCount(), 0);
    assertEquals(statistics.getQueryCacheHitCount(), 1);
    assertEquals(qs.getCacheHitCount(), 1);
    assertEquals(statistics.getQueryCacheMissCount(), 2);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(statistics.getQueryCachePutCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    assertEquals(statistics.getQueryExecutionCount(), 2);
    assertEquals(qs.getExecutionCount(), 2);
    assertEquals(statistics.getEntityFetchCount(), 0);
    assertEquals(qs.getCacheHitCount(), 1);
    assertEquals(statistics.getEntityFetchCount(), 0);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    i = (Item) result.get(0);
    assertTrue(Hibernate.isInitialized(i));
    assertTrue(s.contains(i));
    i.setName("Widget");
    s.flush();
    t.commit();
    s.close();
    // hit -> 2
    // miss -> 2
    // put -> 2
    // 
    // + another invalidation
    assertEquals(es.getInsertCount(), 1);
    assertEquals(es.getUpdateCount(), 1);
    assertEquals(statistics.getQueryCacheHitCount(), 2);
    assertEquals(qs.getCacheHitCount(), 2);
    assertEquals(statistics.getQueryCacheMissCount(), 2);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(statistics.getQueryCachePutCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    assertEquals(statistics.getQueryExecutionCount(), 2);
    assertEquals(qs.getExecutionCount(), 2);
    assertEquals(statistics.getEntityFetchCount(), 0);
    Thread.sleep(200);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    i = (Item) s.get(Item.class, new Long(i.getId()));
    s.delete(i);
    t.commit();
    s.close();
    // hit -> 2
    // miss -> 3
    // put -> 3
    assertEquals(es.getInsertCount(), 1);
    assertEquals(es.getUpdateCount(), 1);
    assertEquals(statistics.getQueryCacheHitCount(), 2);
    assertEquals(qs.getCacheHitCount(), 2);
    assertEquals(statistics.getQueryCacheMissCount(), 3);
    assertEquals(qs.getCacheMissCount(), 3);
    assertEquals(statistics.getQueryCachePutCount(), 3);
    assertEquals(qs.getCachePutCount(), 3);
    assertEquals(statistics.getQueryExecutionCount(), 3);
    assertEquals(qs.getExecutionCount(), 3);
    assertEquals(statistics.getEntityFetchCount(), 0);
    assertEquals(es.getFetchCount(), 0);
    assertEquals(qs.getCacheHitCount(), 2);
    assertEquals(qs.getCacheMissCount(), 3);
    assertEquals(qs.getCachePutCount(), 3);
    assertEquals(qs.getExecutionCount(), 3);
    // check that it was being cached
    assertEquals(es.getFetchCount(), 0);
}
Also used : Transaction(org.hibernate.Transaction) QueryStatistics(org.hibernate.stat.QueryStatistics) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) ArrayList(java.util.ArrayList) List(java.util.List) EntityStatistics(org.hibernate.stat.EntityStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

StatisticsImplementor (org.hibernate.stat.spi.StatisticsImplementor)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HibernateException (org.hibernate.HibernateException)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 EntityStatistics (org.hibernate.stat.EntityStatistics)1 QueryStatistics (org.hibernate.stat.QueryStatistics)1 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)1 StatisticsFactory (org.hibernate.stat.spi.StatisticsFactory)1