Search in sources :

Example 6 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class EhCacheTest method testQueryCacheInvalidation.

@Test
public void testQueryCacheInvalidation() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Item i = new Item();
    i.setName("widget");
    i.setDescription("A really top-quality, full-featured widget.");
    s.persist(i);
    t.commit();
    s.close();
    SecondLevelCacheStatistics slcs = s.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(Item.class.getName());
    assertEquals(slcs.getPutCount(), 1);
    assertEquals(slcs.getElementCountInMemory(), 1);
    assertEquals(slcs.getEntries().size(), 1);
    s = openSession();
    t = s.beginTransaction();
    i = (Item) s.get(Item.class, i.getId());
    assertEquals(slcs.getHitCount(), 1);
    assertEquals(slcs.getMissCount(), 0);
    i.setDescription("A bog standard item");
    t.commit();
    s.close();
    assertEquals(slcs.getPutCount(), 2);
    Object entry = slcs.getEntries().get(i.getId());
    Map map;
    map = getMapFromCacheEntry(entry);
    assertTrue(map.get("description").equals("A bog standard item"));
    assertTrue(map.get("name").equals("widget"));
    // cleanup
    s = openSession();
    t = s.beginTransaction();
    s.delete(i);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class EhCacheTest method testStaleWritesLeaveCacheConsistent.

@SuppressWarnings({ "UnnecessaryBoxing", "UnnecessaryUnboxing", "UnusedAssignment" })
@Test
public void testStaleWritesLeaveCacheConsistent() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    VersionedItem item = new VersionedItem();
    item.setName("steve");
    item.setDescription("steve's item");
    s.save(item);
    txn.commit();
    s.close();
    Long initialVersion = item.getVersion();
    // manually revert the version property
    item.setVersion(Long.valueOf(item.getVersion().longValue() - 1));
    try {
        s = openSession();
        txn = s.beginTransaction();
        s.update(item);
        txn.commit();
        s.close();
        fail("expected stale write to fail");
    } catch (Throwable expected) {
        // expected behavior here
        if (txn != null) {
            try {
                txn.rollback();
            } catch (Throwable ignore) {
            }
        }
    } finally {
        if (s != null && s.isOpen()) {
            try {
                s.close();
            } catch (Throwable ignore) {
            }
        }
    }
    // check the version value in the cache...
    SecondLevelCacheStatistics slcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(VersionedItem.class.getName());
    Object entry = slcs.getEntries().get(item.getId());
    Long cachedVersionValue;
    //		} else
    if (entry.getClass().getName().equals("org.hibernate.cache.ehcache.internal.strategy.AbstractReadWriteEhcacheAccessStrategy$Lock")) {
    //FIXME don't know what to test here
    } else {
        cachedVersionValue = (Long) getMapFromCacheEntry(entry).get("_version");
        assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
    }
    // cleanup
    s = openSession();
    txn = s.beginTransaction();
    item = (VersionedItem) s.load(VersionedItem.class, item.getId());
    s.delete(item);
    txn.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class SecondLevelCacheTest method testCache.

@Test
public void testCache() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        entityManager.persist(new Person());
        entityManager.persist(new Person());
        Person aPerson = new Person();
        aPerson.setName("John Doe");
        aPerson.setCode("unique-code");
        entityManager.persist(aPerson);
        return aPerson;
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa load by id");
        Person person = entityManager.find(Person.class, 1L);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native load by id");
        Session session = entityManager.unwrap(Session.class);
        Person person = session.get(Person.class, 1L);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native load by natural-id");
        Session session = entityManager.unwrap(Session.class);
        Person person = session.byNaturalId(Person.class).using("code", "unique-code").load();
        assertNotNull(person);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa query cache");
        List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.name = :name", Person.class).setParameter("name", "John Doe").setHint("org.hibernate.cacheable", "true").getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache");
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.name = :name").setParameter("name", "John Doe").setCacheable(true).list();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa query cache region");
        List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache");
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").list();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa query cache store mode ");
        List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache store mode");
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").setCacheMode(CacheMode.REFRESH).list();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Statistics statistics = session.getSessionFactory().getStatistics();
        SecondLevelCacheStatistics secondLevelCacheStatistics = statistics.getSecondLevelCacheStatistics("query.cache.person");
        long hitCount = secondLevelCacheStatistics.getHitCount();
        long missCount = secondLevelCacheStatistics.getMissCount();
        double hitRatio = (double) hitCount / (hitCount + missCount);
        return hitRatio;
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache store mode");
        Session session = entityManager.unwrap(Session.class);
        session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Map<String, Object> hints = new HashMap<>();
        hints.put("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE);
        hints.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
        Person person = entityManager.find(Person.class, 1L, hints);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.setCacheMode(CacheMode.REFRESH);
        Person person = session.get(Person.class, 1L);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        List<Person> persons = entityManager.createQuery("select p from Person p", Person.class).setHint(QueryHints.HINT_CACHEABLE, "true").setHint("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE).setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
        entityManager.getEntityManagerFactory().getCache().evict(Person.class);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p from Person p").setCacheable(true).setCacheMode(CacheMode.REFRESH).list();
        session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
    });
}
Also used : HashMap(java.util.HashMap) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class HibernateCacheTest method testQueryCacheInvalidation.

@Test
public void testQueryCacheInvalidation() throws Exception {
    Session s = sessionFactory().openSession();
    Transaction t = s.beginTransaction();
    Item i = new Item();
    i.setName("widget");
    i.setDescription("A really top-quality, full-featured widget.");
    s.persist(i);
    t.commit();
    s.close();
    SecondLevelCacheStatistics slcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(REGION_PREFIX + Item.class.getName());
    assertThat(slcs.getPutCount(), equalTo(1L));
    assertThat(slcs.getEntries().size(), equalTo(1));
    s = sessionFactory().openSession();
    t = s.beginTransaction();
    i = (Item) s.get(Item.class, i.getId());
    assertThat(slcs.getHitCount(), equalTo(1L));
    assertThat(slcs.getMissCount(), equalTo(0L));
    i.setDescription("A bog standard item");
    t.commit();
    s.close();
    assertThat(slcs.getPutCount(), equalTo(2L));
    Object entry = slcs.getEntries().get(i.getId());
    Map map;
    if (entry instanceof Map) {
        map = (Map) entry;
    } else {
        map = ItemValueExtractor.getValue(entry);
    }
    assertThat((String) map.get("description"), equalTo("A bog standard item"));
    assertThat((String) map.get("name"), equalTo("widget"));
    // cleanup
    s = sessionFactory().openSession();
    t = s.beginTransaction();
    s.delete(i);
    t.commit();
    s.close();
}
Also used : Item(org.hibernate.test.domain.Item) VersionedItem(org.hibernate.test.domain.VersionedItem) Transaction(org.hibernate.Transaction) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class HibernateCacheTest method testEmptySecondLevelCacheEntry.

@Test
public void testEmptySecondLevelCacheEntry() throws Exception {
    sessionFactory().getCache().evictEntityRegion(Item.class.getName());
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics(REGION_PREFIX + Item.class.getName());
    Map cacheEntries = statistics.getEntries();
    assertThat(cacheEntries.size(), equalTo(0));
}
Also used : Item(org.hibernate.test.domain.Item) VersionedItem(org.hibernate.test.domain.VersionedItem) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) Map(java.util.Map) Test(org.junit.Test)

Aggregations

SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)32 Test (org.junit.Test)21 Statistics (org.hibernate.stat.Statistics)18 Session (org.hibernate.Session)11 Map (java.util.Map)9 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)9 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)8 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)8 ByRef (org.infinispan.commons.util.ByRef)8 Transaction (org.hibernate.Transaction)6 QueryStatistics (org.hibernate.stat.QueryStatistics)5 EntityManager (javax.persistence.EntityManager)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashMap (java.util.HashMap)3 VersionedItem (org.hibernate.test.domain.VersionedItem)3 StaleStateException (org.hibernate.StaleStateException)2 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)2 Contact (org.hibernate.test.cache.infinispan.functional.entities.Contact)2 Item (org.hibernate.test.domain.Item)2 NotificationFilter (com.thoughtworks.go.domain.NotificationFilter)1