private void deleteCachedItems() throws Exception { withTx(tm, new Callable<Void>() { @Override public Void call() throws Exception { sessionFactory.getCache().evictEntityRegion(Age.class); return null; } }); }
private void queryItems() throws Exception { withTx(tm, new Callable<Void>() { @Override public Void call() throws Exception { Session s = sessionFactory.getCurrentSession(); Query query = s.getNamedQuery(Age.QUERY).setCacheable(true); List<Age> result = (List<Age>) query.list(); assertFalse(result.isEmpty()); return null; } }); }
protected Callable<Object> runWithTx(final TransactionManager tm, final Callable<?> callable) { return () -> TestingUtil.withTx(tm, callable); }
@BeforeMethod public void initialize() throws Exception { // Initialize the cache withTx(tm(), new Callable<Void>() { @Override public Void call() throws Exception { for (int i = 0; i < 100; i++) { cache.put(String.valueOf(i), new Session(String.valueOf(i))); } return null; } }); }
protected Object withTx(int cacheIndex, Callable<Object> c) throws Exception { if (txEnabled) { return TestingUtil.withTx(cache(cacheIndex).getAdvancedCache().getTransactionManager(), c); } else { return c.call(); } }
@BeforeMethod public void insertData() throws Exception { withTx(tm(), (Callable<Void>) () -> { cache.put("1", new Foo("1")); return null; }); }
private Future<Object> executeTransaction(final Cache<Object, Object> cache, final Object key) { return fork(() -> TestingUtil.withTx(cache.getAdvancedCache().getTransactionManager(), () -> cache.put(key, "value"))); }
@Test public void testQuerySkipsNullsInList() throws Exception { withTx(tm(), (Callable<Void>) () -> { cache.remove("1"); // cache will now be out of sync with the index Query query = createQueryBuilder().keyword().onField("bar").matching("1").createQuery(); List list = searchManager.getQuery(query).list(); assert list.size() == 0; return null; }); }
public void testSizeInExplicitTx() throws Exception { assertEquals(0, cache.size()); cache.put("k", "v"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { assertEquals(1, cache.size()); tm.setRollbackOnly(); return null; }); }
public void testSizeWithEntryInContext() throws Exception { cache.put(KEY1, VAL1); assertEquals(1, cache.size()); TransactionManager tm = TestingUtil.getTransactionManager(cache); TestingUtil.withTx(tm, () -> { cache.put(KEY2, VAL2); assertEquals(2, cache.size()); return null; }); } }
public void testQuerySkipsNullsInList() throws Exception { prepareData(); withTx(tm(1), (Callable<Void>) () -> { cache2.remove("2"); // cache will now be out of sync with the index // Query a cache where key "2" is not present in the index Cache queryCache = getKeyLocation("2").equals(cache1) ? cache2 : cache1; searchManager = Search.getSearchManager(queryCache); Query query = createQueryBuilder().keyword().onField("bar").matching("2").createQuery(); List list = searchManager.getQuery(query).list(); assertEquals("Wrong list size.", 0, list.size()); return null; }); }
public void testSizeInExplicitTxWithModification() throws Exception { assertEquals(0, cache.size()); cache.put("k1", "v1"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { assertNull(cache.put("k2", "v2")); assertEquals(2, cache.size()); tm.setRollbackOnly(); return null; }); }
public void testEntrySetIteratorRemoveInExplicitTx() throws Exception { assertEquals(0, cache.size()); cache.put("k1", "v1"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { try (CloseableIterator<Entry<Object, Object>> entryIterator = cache.entrySet().iterator()) { entryIterator.next(); entryIterator.remove(); assertEquals(0, cache.size()); } tm.setRollbackOnly(); return null; }); }
public void testSizeInExplicitTxWithRemoveExistent() throws Exception { assertEquals(0, cache.size()); cache.put("k", "v"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { assertNull(cache.put("exist", "value")); assertEquals(2, cache.size()); assertEquals("value", cache.remove("exist")); assertEquals(1, cache.size()); tm.setRollbackOnly(); return null; }); }
@Test public void testQueryReturnsNullWhenProjectingCacheValue() throws Exception { withTx(tm(), (Callable<Void>) () -> { cache.remove("1"); // cache will now be out of sync with the index Query query = createQueryBuilder().keyword().onField("bar").matching("1").createQuery(); ResultIterator<Object[]> iterator = searchManager.getQuery(query).projection(ProjectionConstants.VALUE, "bar").iterator(new FetchOptions().fetchMode(LAZY)); assertTrue(iterator.hasNext()); Object[] projection = iterator.next(); assertNull(projection[0]); assertEquals("1", projection[1]); return null; }); }
public void testKeySetIteratorRemoveInExplicitTx() throws Exception { assertEquals(0, cache.size()); cache.put("k1", "v1"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { for (CloseableIterator<Object> entryIterator = cache.keySet().iterator(); entryIterator.hasNext(); ) { entryIterator.next(); entryIterator.remove(); assertEquals(0, cache.size()); } tm.setRollbackOnly(); return null; }); }
public void testSizeInExplicitTxWithNonExistent() throws Exception { assertEquals(0, cache.size()); cache.put("k", "v"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { assertNull(cache.get("no-exist")); assertEquals(1, cache.size()); assertNull(cache.put("no-exist", "value")); assertEquals(2, cache.size()); tm.setRollbackOnly(); return null; }); }
public void testPutIfAbsentAfterRemoveInTx() throws Exception { String key = "key_1", old_value = "old_value"; cache.put(key, old_value); assertEquals(old_value, cache.get(key)); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { assertEquals(old_value, cache.remove(key)); assertNull(cache.get(key)); assertEquals(cache.putIfAbsent(key, "new_value"), null); tm.setRollbackOnly(); return null; }); assertEquals(old_value, cache.get(key)); }
public void testSizeInExplicitTxWithRemoveNonExistent() throws Exception { assertEquals(0, cache.size()); cache.put("k", "v"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { assertNull(cache.remove("no-exist")); assertEquals( 1, cache.size()); assertNull(cache.put("no-exist", "value")); assertEquals(2, cache.size()); tm.setRollbackOnly(); return null; }); }