Search in sources :

Example 1 with ByRef

use of org.infinispan.commons.util.ByRef 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 2 with ByRef

use of org.infinispan.commons.util.ByRef in project hibernate-orm by hibernate.

the class ReadWriteTest method testAddNewManyToManyPropertyRefNoInitFlushInitLeaveCacheConsistent.

@Test
public void testAddNewManyToManyPropertyRefNoInitFlushInitLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> otherItemId = new ByRef<>(null);
    withTxSession(s -> {
        OtherItem otherItem = new OtherItem();
        otherItem.setName("steve");
        s.save(otherItem);
        otherItemId.set(otherItem.getId());
    });
    // create an element for otherItem.bagOfItems
    Item item = new Item();
    item.setName("element");
    item.setDescription("element Item");
    withTxSession(s -> {
        OtherItem otherItem = s.get(OtherItem.class, otherItemId.get());
        assertFalse(Hibernate.isInitialized(otherItem.getBagOfItems()));
        otherItem.addItemToBag(item);
        assertFalse(Hibernate.isInitialized(otherItem.getBagOfItems()));
        s.persist(item);
        s.flush();
        Hibernate.initialize(otherItem.getBagOfItems());
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        OtherItem otherItem = s.get(OtherItem.class, otherItemId.get());
        Hibernate.initialize(otherItem.getBagOfItems());
        assertTrue(otherItem.getBagOfItems().isEmpty());
        s.delete(otherItem);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) 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)

Example 3 with ByRef

use of org.infinispan.commons.util.ByRef in project hibernate-orm by hibernate.

the class ReadWriteTest method testQueryCacheInvalidation.

@Test
public void testQueryCacheInvalidation() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
    sessionFactory().getCache().evictEntityRegion(Item.class.getName());
    TIME_SERVICE.advance(1);
    assertEquals(0, slcs.getPutCount());
    assertEquals(0, slcs.getElementCountInMemory());
    assertEquals(0, slcs.getEntries().size());
    ByRef<Long> idRef = new ByRef<>(null);
    withTxSession(s -> {
        Item item = new Item();
        item.setName("widget");
        item.setDescription("A really top-quality, full-featured widget.");
        s.persist(item);
        idRef.set(item.getId());
    });
    assertEquals(1, slcs.getPutCount());
    assertEquals(1, slcs.getElementCountInMemory());
    assertEquals(1, slcs.getEntries().size());
    withTxSession(s -> {
        Item item = s.get(Item.class, idRef.get());
        assertEquals(slcs.getHitCount(), 1);
        assertEquals(slcs.getMissCount(), 0);
        item.setDescription("A bog standard item");
    });
    assertEquals(slcs.getPutCount(), 2);
    CacheEntry entry = (CacheEntry) slcs.getEntries().get(idRef.get());
    Serializable[] ser = entry.getDisassembledState();
    assertTrue(ser[0].equals("widget"));
    assertTrue(ser[1].equals("A bog standard item"));
    withTxSession(s -> {
        Item item = s.load(Item.class, idRef.get());
        s.delete(item);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) Serializable(java.io.Serializable) 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)

Example 4 with ByRef

use of org.infinispan.commons.util.ByRef in project hibernate-orm by hibernate.

the class VersionedTest method testStaleRead.

protected ByRef<Object> testStaleRead(BiConsumer<Session, Item> consumer) throws Exception {
    AtomicReference<Exception> synchronizationException = new AtomicReference<>();
    CountDownLatch syncLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);
    Future<Boolean> action = executor.submit(() -> withTxSessionApply(s -> {
        try {
            ((SharedSessionContractImplementor) s).getTransactionCoordinator().getLocalSynchronizations().registerSynchronization(new Synchronization() {

                @Override
                public void beforeCompletion() {
                }

                @Override
                public void afterCompletion(int i) {
                    syncLatch.countDown();
                    try {
                        awaitOrThrow(commitLatch);
                    } catch (Exception e) {
                        synchronizationException.set(e);
                    }
                }
            });
            Item item = s.load(Item.class, itemId);
            consumer.accept(s, item);
            s.flush();
        } catch (StaleStateException e) {
            log.info("Exception thrown: ", e);
            markRollbackOnly(s);
            return false;
        } catch (PessimisticLockException e) {
            log.info("Exception thrown: ", e);
            markRollbackOnly(s);
            return false;
        }
        return true;
    }));
    awaitOrThrow(syncLatch);
    ByRef<Object> entryRef = new ByRef<>(null);
    try {
        withTxSession(s -> {
            Item item = s.load(Item.class, itemId);
            assertEquals("Original item", item.getDescription());
            entryRef.set(assertSingleCacheEntry());
        });
    } finally {
        commitLatch.countDown();
    }
    assertTrue(action.get(WAIT_TIMEOUT, TimeUnit.SECONDS));
    assertNull(synchronizationException.get());
    return entryRef;
}
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) ByRef(org.infinispan.commons.util.ByRef) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Synchronization(javax.transaction.Synchronization) PessimisticLockException(org.hibernate.PessimisticLockException) StaleStateException(org.hibernate.StaleStateException) PessimisticLockException(org.hibernate.PessimisticLockException) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) StaleStateException(org.hibernate.StaleStateException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 5 with ByRef

use of org.infinispan.commons.util.ByRef in project hibernate-orm by hibernate.

the class ReadWriteTest method testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent.

@Test
public void testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> itemId = new ByRef<>(null);
    saveItem(itemId);
    // create an element for item.bagOfItems
    Item itemElement = new Item();
    itemElement.setName("element");
    itemElement.setDescription("element item");
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        item.addItemToBag(itemElement);
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        s.persist(itemElement);
        s.flush();
        Hibernate.initialize(item.getBagOfItems());
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        Hibernate.initialize(item.getBagOfItems());
        assertTrue(item.getBagOfItems().isEmpty());
        s.delete(item);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) 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)

Aggregations

ByRef (org.infinispan.commons.util.ByRef)10 Test (org.junit.Test)10 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)9 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)9 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)8 Statistics (org.hibernate.stat.Statistics)8 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)8 TestForIssue (org.hibernate.testing.TestForIssue)4 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)2 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1