Search in sources :

Example 21 with SecondLevelCacheStatistics

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

the class ReadWriteTest method testStaleWritesLeaveCacheConsistent.

@Test
public void testStaleWritesLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    ByRef<VersionedItem> itemRef = new ByRef<>(null);
    withTxSession(s -> {
        VersionedItem item = new VersionedItem();
        item.setName("steve");
        item.setDescription("steve's item");
        s.save(item);
        itemRef.set(item);
    });
    final VersionedItem item = itemRef.get();
    Long initialVersion = item.getVersion();
    // manually revert the version property
    item.setVersion(new Long(item.getVersion().longValue() - 1));
    try {
        withTxSession(s -> s.update(item));
        fail("expected stale write to fail");
    } catch (Exception e) {
        log.debug("Rollback was expected", e);
    }
    // check the version value in the cache...
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(VersionedItem.class.getName());
    Object entry = slcs.getEntries().get(item.getId());
    Long cachedVersionValue;
    cachedVersionValue = (Long) ((CacheEntry) entry).getVersion();
    assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
    withTxSession(s -> {
        VersionedItem item2 = s.load(VersionedItem.class, item.getId());
        s.delete(item2);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 22 with SecondLevelCacheStatistics

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

the class ReadWriteTest method testPersistEntityFlushEvictGetRollbackNotInEntityCache.

@Test
@TestForIssue(jiraKey = "HHH-5690")
public void testPersistEntityFlushEvictGetRollbackNotInEntityCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
    ByRef<Long> itemId = new ByRef<>(null);
    withTxSession(s -> {
        Item item = new Item();
        item.setName("steve");
        item.setDescription("steve's item");
        s.persist(item);
        s.flush();
        itemId.set(item.getId());
        s.evict(item);
        assertEquals(slcs.getHitCount(), 0);
        item = s.get(Item.class, item.getId());
        assertNotNull(item);
        markRollbackOnly(s);
    });
    // item should not be in entity cache.
    //slcs = stats.getSecondLevelCacheStatistics( Item.class.getName() );
    assertEquals(Collections.EMPTY_MAP, slcs.getEntries());
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertNull(item);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 23 with SecondLevelCacheStatistics

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

the class ReadWriteTest method testPersistEntityFlushRollbackNotInEntityCache.

@Test
@TestForIssue(jiraKey = "HHH-5690")
public void testPersistEntityFlushRollbackNotInEntityCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
    ByRef<Long> itemId = new ByRef<>(null);
    withTxSession(s -> {
        Item item = new Item();
        item.setName("steve");
        item.setDescription("steve's item");
        s.persist(item);
        s.flush();
        itemId.set(item.getId());
        markRollbackOnly(s);
    });
    // item should not be in entity cache.
    assertEquals(Collections.EMPTY_MAP, slcs.getEntries());
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertNull(item);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 24 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project wildfly by wildfly.

the class StatefulBean method getEmployeesInMemory.

@Override
public long getEmployeesInMemory() {
    Session session = em.unwrap(Session.class);
    String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        if (name.contains(Employee.class.getName())) {
            SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(name);
            return stats.getElementCountInMemory();
        }
    }
    return -1;
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session)

Example 25 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project wildfly by wildfly.

the class SFSB2LC method sameSessionCheck.

/**
     * Checking entity 2LC in one EntityManager session
     */
public String sameSessionCheck(String CACHE_REGION_NAME) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics emp2LCStats = stats.getSecondLevelCacheStatistics(CACHE_REGION_NAME + "Employee");
    try {
        // add new entities and check if they are put in 2LC
        createEmployee(em, "Peter", "Ostrava", 2);
        createEmployee(em, "Tom", "Brno", 3);
        assertEquals("There are 2 puts in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getPutCount());
        // loading all Employee entities should put in 2LC all Employee
        List<?> empList = getAllEmployeesQuery(em);
        assertEquals("There are 2 entities.", empList.size(), 2);
        assertEquals("There are 2 entities in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getElementCountInMemory());
        // clear session
        em.clear();
        // entity should be loaded from 2L cache, we'are expecting hit in 2L cache
        Employee emp = getEmployee(em, 2);
        assertNotNull("Employee returned", emp);
        assertEquals("Expected 1 hit in cache" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics)

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