Search in sources :

Example 1 with Item

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

the class AbstractNonInvalidationTest method insertAndClearCache.

@Before
public void insertAndClearCache() throws Exception {
    region = TEST_SESSION_ACCESS.getRegion(sessionFactory(), Item.class.getName());
    entityCache = region.getCache();
    timeout = TestRegionFactoryProvider.load().findRegionFactory(sessionFactory().getCache()).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.infinispan.test.hibernate.cache.commons.functional.entities.Item) Before(org.junit.Before)

Example 2 with Item

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

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());
        // item is cached on insert.
        // assertNotNull( slcs.getEntries().get( item.getId() ) );
        s.evict(item);
        assertEquals(slcs.getHitCount(), 0);
        item = s.get(Item.class, item.getId());
        assertNotNull(item);
        // assertEquals( slcs.getHitCount(), 1 );
        // assertNotNull( slcs.getEntries().get( item.getId() ) );
        markRollbackOnly(s);
    });
    // item should not be in entity cache.
    // slcs = stats.getSecondLevelCacheStatistics( Item.class.getName() );
    assertEquals(0, getNumberOfItems());
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertNull(item);
    });
}
Also used : OtherItem(org.infinispan.test.hibernate.cache.commons.functional.entities.OtherItem) Item(org.infinispan.test.hibernate.cache.commons.functional.entities.Item) VersionedItem(org.infinispan.test.hibernate.cache.commons.functional.entities.VersionedItem) 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 3 with Item

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

the class ReadWriteTest method testCollectionCache.

@Test
public void testCollectionCache() throws Exception {
    final Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    final Item item = new Item("chris", "Chris's Item");
    final Item another = new Item("another", "Owned Item");
    item.addItem(another);
    withTxSession(s -> {
        s.persist(item);
        s.persist(another);
    });
    // The collection has been removed, but we can't add it again immediately using putFromLoad
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        Item loaded = s.load(Item.class, item.getId());
        assertEquals(1, loaded.getItems().size());
    });
    String itemsRegionName = Item.class.getName() + ".items";
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(itemsRegionName);
    assertEquals(1, cStats.getElementCountInMemory());
    withTxSession(s -> {
        Item loadedWithCachedCollection = (Item) s.load(Item.class, item.getId());
        stats.logSummary();
        assertEquals(item.getName(), loadedWithCachedCollection.getName());
        assertEquals(item.getItems().size(), loadedWithCachedCollection.getItems().size());
        assertEquals(1, cStats.getHitCount());
        assertEquals(1, TEST_SESSION_ACCESS.getRegion(sessionFactory(), itemsRegionName).getElementCountInMemory());
        Item itemElement = loadedWithCachedCollection.getItems().iterator().next();
        itemElement.setOwner(null);
        loadedWithCachedCollection.getItems().clear();
        s.delete(itemElement);
        s.delete(loadedWithCachedCollection);
    });
}
Also used : OtherItem(org.infinispan.test.hibernate.cache.commons.functional.entities.OtherItem) Item(org.infinispan.test.hibernate.cache.commons.functional.entities.Item) VersionedItem(org.infinispan.test.hibernate.cache.commons.functional.entities.VersionedItem) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 4 with Item

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

the class InvalidationTest method testConcurrentRemoveAndPutFromLoad.

@Test
@TestForIssue(jiraKey = "HHH-9868")
public void testConcurrentRemoveAndPutFromLoad() throws Exception {
    final Item item = new Item("chris", "Chris's Item");
    withTxSession(s -> {
        s.persist(item);
    });
    Phaser deletePhaser = new Phaser(2);
    Phaser getPhaser = new Phaser(2);
    HookInterceptor hook = new HookInterceptor();
    AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class);
    extractInterceptorChain(pendingPutsCache).addInterceptor(hook, 0);
    AtomicBoolean getThreadBlockedInDB = new AtomicBoolean(false);
    Thread deleteThread = new Thread(() -> {
        try {
            withTxSession(s -> {
                Item loadedItem = s.get(Item.class, item.getId());
                assertNotNull(loadedItem);
                arriveAndAwait(deletePhaser, 2000);
                arriveAndAwait(deletePhaser, 2000);
                log.trace("Item loaded");
                s.delete(loadedItem);
                s.flush();
                log.trace("Item deleted");
                // start get-thread here
                arriveAndAwait(deletePhaser, 2000);
                // we need longer timeout since in non-MVCC DBs the get thread
                // can be blocked
                arriveAndAwait(deletePhaser, 4000);
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, "delete-thread");
    Thread getThread = new Thread(() -> {
        try {
            withTxSession(s -> {
                // DB load should happen before the record is deleted,
                // putFromLoad should happen after deleteThread ends
                Item loadedItem = s.get(Item.class, item.getId());
                if (getThreadBlockedInDB.get()) {
                    assertNull(loadedItem);
                } else {
                    assertNotNull(loadedItem);
                }
            });
        } catch (PessimisticLockException e) {
            // (delete-thread has ITEMS table write-locked and we try to acquire read-lock)
            try {
                arriveAndAwait(getPhaser, 2000);
                arriveAndAwait(getPhaser, 2000);
            } catch (Exception e1) {
                throw new RuntimeException(e1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, "get-thread");
    deleteThread.start();
    // deleteThread loads the entity
    arriveAndAwait(deletePhaser, 2000);
    withTx(() -> {
        sessionFactory().getCache().evictEntity(Item.class, item.getId());
        assertFalse(sessionFactory().getCache().containsEntity(Item.class, item.getId()));
        return null;
    });
    arriveAndAwait(deletePhaser, 2000);
    // delete thread invalidates PFER
    arriveAndAwait(deletePhaser, 2000);
    // get thread gets the entity from DB
    hook.block(getPhaser, getThread);
    getThread.start();
    try {
        arriveAndAwait(getPhaser, 2000);
    } catch (TimeoutException e) {
        getThreadBlockedInDB.set(true);
    }
    arriveAndAwait(deletePhaser, 2000);
    // delete thread finishes the remove from DB and cache
    deleteThread.join();
    hook.unblock();
    arriveAndAwait(getPhaser, 2000);
    // get thread puts the entry into cache
    getThread.join();
    assertNoInvalidators(pendingPutsCache);
    withTxSession(s -> {
        Item loadedItem = s.get(Item.class, item.getId());
        assertNull(loadedItem);
    });
}
Also used : Item(org.infinispan.test.hibernate.cache.commons.functional.entities.Item) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AdvancedCache(org.infinispan.AdvancedCache) Phaser(java.util.concurrent.Phaser) TimeoutException(java.util.concurrent.TimeoutException) PessimisticLockException(org.hibernate.PessimisticLockException) PessimisticLockException(org.hibernate.PessimisticLockException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with Item

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

the class InvalidationTest method testFailedRemove.

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

Aggregations

Item (org.infinispan.test.hibernate.cache.commons.functional.entities.Item)31 Test (org.junit.Test)28 OtherItem (org.infinispan.test.hibernate.cache.commons.functional.entities.OtherItem)19 Statistics (org.hibernate.stat.Statistics)14 VersionedItem (org.infinispan.test.hibernate.cache.commons.functional.entities.VersionedItem)11 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)10 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 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 ReadWriteKeyCommand (org.infinispan.commands.functional.ReadWriteKeyCommand)3 Assert.assertEquals (org.junit.Assert.assertEquals)3