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");
}
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);
});
}
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);
});
}
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);
});
}
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);
}
Aggregations