Search in sources :

Example 21 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class InvalidationTest method testFailedInsert.

@TestForIssue(jiraKey = "HHH-11304")
@Test
public void testFailedInsert() throws Exception {
    AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class);
    assertNoInvalidators(pendingPutsCache);
    withTxSession(s -> {
        Item i = new Item("inserted", "bar");
        s.persist(i);
        s.flush();
        s.getTransaction().setRollbackOnly();
    });
    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 22 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class MultiTenancyTest method testMultiTenancy.

@Test
public void testMultiTenancy() throws Exception {
    final Item item = new Item("my item", "description");
    withTxSession(sessionFactory().withOptions().tenantIdentifier(DB1), s -> s.persist(item));
    for (int i = 0; i < 5; ++i) {
        // make sure we get something cached
        withTxSession(sessionFactory().withOptions().tenantIdentifier(DB1), s -> {
            Item item2 = s.get(Item.class, item.getId());
            assertNotNull(item2);
            assertEquals(item.getName(), item2.getName());
        });
    }
    // The table ITEMS is not created in DB2 - we would get just an exception
    //        for (int i = 0; i < 5; ++i) { // make sure we get something cached
    //            withTx(tm, new Callable<Void>() {
    //                @Override
    //                public Void call() throws Exception {
    //                    Session s = sessionFactory().withOptions().tenantIdentifier(DB2).openSession();
    //                    s.getTransaction().begin();
    //                    Item item2 = s.get(Item.class, id);
    //                    s.getTransaction().commit();
    //                    s.close();
    //                    assertNull(item2);
    //                    return null;
    //                }
    //            });
    //        }
    EntityRegionImpl region = (EntityRegionImpl) sessionFactory().getSecondLevelCacheRegion(Item.class.getName());
    AdvancedCache localCache = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL);
    assertEquals(1, localCache.size());
    try (CloseableIterator iterator = localCache.keySet().iterator()) {
        assertEquals("CacheKeyImplementation", iterator.next().getClass().getSimpleName());
    }
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) EntityRegionImpl(org.hibernate.cache.infinispan.entity.EntityRegionImpl) CloseableIterator(org.infinispan.commons.util.CloseableIterator) AdvancedCache(org.infinispan.AdvancedCache) Test(org.junit.Test)

Example 23 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

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.remove(item2);
        s.flush();
        s.getTransaction().setRollbackOnly();
    });
    assertNoInvalidators(pendingPutsCache);
    withTxSession(s -> {
        Item item3 = s.load(Item.class, item.getId());
        assertEquals("before-remove", 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 24 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class NoTenancyTest method testNoTenancy.

@Test
public void testNoTenancy() throws Exception {
    final Item item = new Item("my item", "description");
    withTxSession(s -> s.persist(item));
    for (int i = 0; i < 5; ++i) {
        // make sure we get something cached
        withTxSession(s -> {
            Item item2 = s.get(Item.class, item.getId());
            assertNotNull(item2);
            assertEquals(item.getName(), item2.getName());
        });
    }
    EntityRegionImpl region = (EntityRegionImpl) sessionFactory().getSecondLevelCacheRegion(Item.class.getName());
    AdvancedCache localCache = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL);
    assertEquals(1, localCache.size());
    try (CloseableIterator iterator = localCache.keySet().iterator()) {
        assertEquals(sessionFactory().getClassMetadata(Item.class).getIdentifierType().getReturnedClass(), iterator.next().getClass());
    }
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) EntityRegionImpl(org.hibernate.cache.infinispan.entity.EntityRegionImpl) CloseableIterator(org.infinispan.commons.util.CloseableIterator) AdvancedCache(org.infinispan.AdvancedCache) Test(org.junit.Test)

Example 25 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class TombstoneCallInterceptor method visitSizeCommand.

@Override
public Object visitSizeCommand(InvocationContext ctx, SizeCommand command) throws Throwable {
    Set<Flag> flags = command.getFlags();
    int size = 0;
    AdvancedCache decoratedCache = cache.getAdvancedCache();
    if (flags != null) {
        decoratedCache = decoratedCache.withFlags(flags.toArray(new Flag[flags.size()]));
    }
    // In non-transactional caches we don't care about context
    CloseableIterable<CacheEntry<Object, Object>> iterable = decoratedCache.filterEntries(Tombstone.EXCLUDE_TOMBSTONES).converter(NullValueConverter.getInstance());
    try {
        for (CacheEntry<Object, Object> entry : iterable) {
            if (size++ == Integer.MAX_VALUE) {
                return Integer.MAX_VALUE;
            }
        }
    } finally {
        iterable.close();
    }
    return size;
}
Also used : AdvancedCache(org.infinispan.AdvancedCache) CacheEntry(org.infinispan.container.entries.CacheEntry) Flag(org.infinispan.context.Flag)

Aggregations

AdvancedCache (org.infinispan.AdvancedCache)32 Test (org.junit.Test)20 Properties (java.util.Properties)12 EntityRegionImpl (org.hibernate.cache.infinispan.entity.EntityRegionImpl)10 Configuration (org.infinispan.configuration.cache.Configuration)9 QueryResultsRegionImpl (org.hibernate.cache.infinispan.query.QueryResultsRegionImpl)7 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)7 TestForIssue (org.hibernate.testing.TestForIssue)7 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)6 CollectionRegionImpl (org.hibernate.cache.infinispan.collection.CollectionRegionImpl)5 TimestampsRegionImpl (org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl)5 List (java.util.List)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Session (org.hibernate.Session)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 TimeUnit (java.util.concurrent.TimeUnit)3