Search in sources :

Example 1 with VersionedItem

use of org.infinispan.test.hibernate.cache.commons.functional.entities.VersionedItem in project infinispan by infinispan.

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...
    Object entry = getEntry(VersionedItem.class.getName(), item.getId());
    assertNotNull(entry);
    Long cachedVersionValue = (Long) ((CacheEntry) entry).getVersion();
    assertNotNull(cachedVersionValue);
    assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
    withTxSession(s -> {
        VersionedItem item2 = s.load(VersionedItem.class, item.getId());
        s.delete(item2);
    });
}
Also used : VersionedItem(org.infinispan.test.hibernate.cache.commons.functional.entities.VersionedItem) ByRef(org.infinispan.commons.util.ByRef) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Aggregations

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