Search in sources :

Example 16 with SoftLock

use of org.hibernate.cache.spi.access.SoftLock in project hibernate-orm by hibernate.

the class HibernateCacheTest method testStaleWritesLeaveCacheConsistent.

// @Test
// public void testEmptySecondLevelCacheEntry() throws Exception {
// sessionFactory().getCache().evictEntityRegion( Item.class.getName() );
// Statistics stats = sessionFactory().getStatistics();
// stats.clear();
// CacheRegionStatistics statistics = stats.getDomainDataRegionStatistics( Item.class.getName() );
// Map cacheEntries = statistics.getEntries();
// assertThat( cacheEntries.size(), equalTo( 0 ) );
// }
@Test
public void testStaleWritesLeaveCacheConsistent() {
    Session s = sessionFactory().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(item.getVersion() - 1);
    try {
        s = sessionFactory().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...
    // CacheRegionStatistics slcs = sessionFactory().getStatistics()
    // .getDomainDataRegionStatistics( VersionedItem.class.getName() );
    // assertNotNull(slcs);
    // final Map entries = slcs.getEntries();
    // Object entry = entries.get( item.getId() );
    // Long cachedVersionValue;
    // if ( entry instanceof SoftLock ) {
    // //FIXME don't know what to test here
    // //cachedVersionValue = new Long( ( (ReadWriteCache.Lock) entry).getUnlockTimestamp() );
    // }
    // else {
    // cachedVersionValue = (Long) ( (Map) entry ).get( "_version" );
    // assertThat( initialVersion, equalTo( cachedVersionValue ) );
    // }
    final DomainDataRegionTemplate region = (DomainDataRegionTemplate) sessionFactory().getMetamodel().entityPersister(Item.class).getCacheAccessStrategy().getRegion();
    final Object fromCache = region.getCacheStorageAccess().getFromCache(region.getEffectiveKeysFactory().createEntityKey(item.getId(), sessionFactory().getMetamodel().entityPersister(Item.class), sessionFactory(), null), (SharedSessionContractImplementor) s);
    assertTrue(fromCache == null || fromCache instanceof SoftLock);
    // cleanup
    s = sessionFactory().openSession();
    txn = s.beginTransaction();
    item = s.load(VersionedItem.class, item.getId());
    s.delete(item);
    txn.commit();
    s.close();
}
Also used : VersionedItem(org.hibernate.jcache.test.domain.VersionedItem) Item(org.hibernate.jcache.test.domain.Item) Transaction(org.hibernate.Transaction) VersionedItem(org.hibernate.jcache.test.domain.VersionedItem) DomainDataRegionTemplate(org.hibernate.cache.spi.support.DomainDataRegionTemplate) Session(org.hibernate.Session) SoftLock(org.hibernate.cache.spi.access.SoftLock) Test(org.junit.Test) BaseFunctionalTest(org.hibernate.jcache.test.BaseFunctionalTest)

Aggregations

SoftLock (org.hibernate.cache.spi.access.SoftLock)16 Session (org.hibernate.Session)3 Transaction (org.hibernate.Transaction)3 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)3 TestSynchronization (org.hibernate.test.cache.infinispan.util.TestSynchronization)3 Serializable (java.io.Serializable)2 CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Test (org.junit.Test)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Predicate (java.util.function.Predicate)1