Search in sources :

Example 1 with VersionedItem

use of org.hibernate.test.cache.infinispan.functional.entities.VersionedItem 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)

Aggregations

CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)1 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)1 Statistics (org.hibernate.stat.Statistics)1 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)1 ByRef (org.infinispan.commons.util.ByRef)1 Test (org.junit.Test)1