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