Search in sources :

Example 1 with Item

use of org.hibernate.test.cache.infinispan.functional.entities.Item in project hibernate-orm by hibernate.

the class AbstractNonInvalidationTest method insertAndClearCache.

@Before
public void insertAndClearCache() throws Exception {
    region = sessionFactory().getSecondLevelCacheRegion(Item.class.getName());
    entityCache = ((EntityRegionImpl) region).getCache();
    timeout = ((EntityRegionImpl) region).getRegionFactory().getPendingPutsCacheConfiguration().expiration().maxIdle();
    Item item = new Item("my item", "Original item");
    withTxSession(s -> s.persist(item));
    entityCache.clear();
    assertEquals("Cache is not empty", Collections.EMPTY_SET, entityCache.keySet());
    itemId = item.getId();
    log.info("Insert and clear finished");
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) EntityRegionImpl(org.hibernate.cache.infinispan.entity.EntityRegionImpl) Before(org.junit.Before)

Example 2 with Item

use of org.hibernate.test.cache.infinispan.functional.entities.Item in project hibernate-orm by hibernate.

the class InvalidationTest method testFailedUpdate.

@TestForIssue(jiraKey = "HHH-11304")
@Test
public void testFailedUpdate() throws Exception {
    AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class);
    assertNoInvalidators(pendingPutsCache);
    final Item item = new Item("before-update", "bar");
    withTxSession(s -> s.persist(item));
    withTxSession(s -> {
        Item item2 = s.load(Item.class, item.getId());
        assertEquals("before-update", item2.getName());
        item2.setName("after-update");
        s.persist(item2);
        s.flush();
        s.flush();
        s.getTransaction().setRollbackOnly();
    });
    assertNoInvalidators(pendingPutsCache);
    withTxSession(s -> {
        Item item3 = s.load(Item.class, item.getId());
        assertEquals("before-update", item3.getName());
        s.remove(item3);
    });
    assertNoInvalidators(pendingPutsCache);
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) AdvancedCache(org.infinispan.AdvancedCache) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with Item

use of org.hibernate.test.cache.infinispan.functional.entities.Item in project hibernate-orm by hibernate.

the class VersionedTest method testEvictUpdateExpiration.

@Test
public void testEvictUpdateExpiration() throws Exception {
    // since the timestamp for update is based on session open/tx begin time, we have to do this sequentially
    sessionFactory().getCache().evictEntity(Item.class, itemId);
    assertSingleEmpty();
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        Item item = s.load(Item.class, itemId);
        item.setDescription("Updated item");
        s.update(item);
    });
    assertSingleCacheEntry();
    TIME_SERVICE.advance(timeout + 1);
    assertSingleCacheEntry();
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) Test(org.junit.Test)

Example 4 with Item

use of org.hibernate.test.cache.infinispan.functional.entities.Item in project hibernate-orm by hibernate.

the class VersionedTest method testUpdateRolledBack.

@Test
public void testUpdateRolledBack() throws Exception {
    ByRef<Object> entryRef = new ByRef<>(null);
    withTxSession(s -> {
        Item item = s.load(Item.class, itemId);
        item.getDescription();
        Object prevEntry = assertSingleCacheEntry();
        entryRef.set(prevEntry);
        item.setDescription("Updated item");
        s.update(item);
        assertEquals(prevEntry, assertSingleCacheEntry());
        s.flush();
        assertEquals(prevEntry, assertSingleCacheEntry());
        markRollbackOnly(s);
    });
    assertEquals(entryRef.get(), assertSingleCacheEntry());
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) ByRef(org.infinispan.commons.util.ByRef) Test(org.junit.Test)

Example 5 with Item

use of org.hibernate.test.cache.infinispan.functional.entities.Item in project hibernate-orm by hibernate.

the class VersionedTest method testCollectionUpdate.

@Test
public void testCollectionUpdate() throws Exception {
    // the first insert puts VersionedEntry(null, null, timestamp), so we have to wait a while to cache the entry
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        Item item = s.load(Item.class, itemId);
        OtherItem otherItem = new OtherItem();
        otherItem.setName("Other 1");
        s.persist(otherItem);
        item.addOtherItem(otherItem);
    });
    withTxSession(s -> {
        Item item = s.load(Item.class, itemId);
        Set<OtherItem> otherItems = item.getOtherItems();
        assertFalse(otherItems.isEmpty());
        otherItems.remove(otherItems.iterator().next());
    });
    AdvancedCache collectionCache = ((BaseTransactionalDataRegion) sessionFactory().getSecondLevelCacheRegion(Item.class.getName() + ".otherItems")).getCache();
    CountDownLatch putFromLoadLatch = new CountDownLatch(1);
    AtomicBoolean committing = new AtomicBoolean(false);
    CollectionUpdateTestInterceptor collectionUpdateTestInterceptor = new CollectionUpdateTestInterceptor(putFromLoadLatch);
    AnotherCollectionUpdateTestInterceptor anotherInterceptor = new AnotherCollectionUpdateTestInterceptor(putFromLoadLatch, committing);
    collectionCache.addInterceptor(collectionUpdateTestInterceptor, collectionCache.getInterceptorChain().size() - 1);
    collectionCache.addInterceptor(anotherInterceptor, 0);
    TIME_SERVICE.advance(1);
    Future<Boolean> addFuture = executor.submit(() -> withTxSessionApply(s -> {
        collectionUpdateTestInterceptor.updateLatch.await();
        Item item = s.load(Item.class, itemId);
        OtherItem otherItem = new OtherItem();
        otherItem.setName("Other 2");
        s.persist(otherItem);
        item.addOtherItem(otherItem);
        committing.set(true);
        return true;
    }));
    Future<Boolean> readFuture = executor.submit(() -> withTxSessionApply(s -> {
        Item item = s.load(Item.class, itemId);
        assertTrue(item.getOtherItems().isEmpty());
        return true;
    }));
    addFuture.get();
    readFuture.get();
    collectionCache.removeInterceptor(CollectionUpdateTestInterceptor.class);
    collectionCache.removeInterceptor(AnotherCollectionUpdateTestInterceptor.class);
    withTxSession(s -> assertFalse(s.load(Item.class, itemId).getOtherItems().isEmpty()));
}
Also used : BaseTransactionalDataRegion(org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion) Arrays(java.util.Arrays) VersionedEntry(org.hibernate.cache.infinispan.util.VersionedEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Session(org.hibernate.Session) Caches(org.hibernate.cache.infinispan.util.Caches) AtomicReference(java.util.concurrent.atomic.AtomicReference) Future(java.util.concurrent.Future) PessimisticLockException(org.hibernate.PessimisticLockException) InvocationContext(org.infinispan.context.InvocationContext) AdvancedCache(org.infinispan.AdvancedCache) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) Synchronization(javax.transaction.Synchronization) StaleStateException(org.hibernate.StaleStateException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ByRef(org.infinispan.commons.util.ByRef) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) BaseCustomInterceptor(org.infinispan.interceptors.base.BaseCustomInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Flag(org.infinispan.context.Flag) Assert.assertFalse(org.junit.Assert.assertFalse) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) BaseTransactionalDataRegion(org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion) AdvancedCache(org.infinispan.AdvancedCache) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Aggregations

Item (org.hibernate.test.cache.infinispan.functional.entities.Item)31 Test (org.junit.Test)28 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)19 Statistics (org.hibernate.stat.Statistics)14 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)12 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 ByRef (org.infinispan.commons.util.ByRef)10 TestForIssue (org.hibernate.testing.TestForIssue)9 AdvancedCache (org.infinispan.AdvancedCache)8 Arrays (java.util.Arrays)3 List (java.util.List)3 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 Future (java.util.concurrent.Future)3 TimeUnit (java.util.concurrent.TimeUnit)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 PessimisticLockException (org.hibernate.PessimisticLockException)3 Session (org.hibernate.Session)3 EntityRegionImpl (org.hibernate.cache.infinispan.entity.EntityRegionImpl)3