use of org.infinispan.manager.CacheContainer in project hibernate-orm by hibernate.
the class SessionRefreshTest method testRefreshAfterExternalChange.
@Test
public void testRefreshAfterExternalChange() throws Exception {
// First session factory uses a cache
CacheContainer localManager = ClusterAwareRegionFactory.getCacheManager(DualNodeTest.LOCAL);
localCache = localManager.getCache(Account.class.getName());
SessionFactory localFactory = sessionFactory();
// Second session factory doesn't; just needs a transaction manager
SessionFactory remoteFactory = secondNodeEnvironment().getSessionFactory();
AccountDAO dao0 = new AccountDAO(useJta, localFactory);
AccountDAO dao1 = new AccountDAO(useJta, remoteFactory);
Integer id = new Integer(1);
dao0.createAccount(dao0.getSmith(), id, new Integer(5), DualNodeTest.LOCAL);
// Basic sanity check
Account acct1 = dao1.getAccount(id);
assertNotNull(acct1);
assertEquals(DualNodeTest.LOCAL, acct1.getBranch());
// This dao's session factory isn't caching, so cache won't see this change
dao1.updateAccountBranch(id, DualNodeTest.REMOTE);
// dao1's session doesn't touch the cache,
// so reading from dao0 should show a stale value from the cache
// (we check to confirm the cache is used)
Account acct0 = dao0.getAccount(id);
assertNotNull(acct0);
assertEquals(DualNodeTest.LOCAL, acct0.getBranch());
log.debug("Contents when re-reading from local: " + TestingUtil.printCache(localCache));
// Now call session.refresh and confirm we get the correct value
acct0 = dao0.getAccountWithRefresh(id);
assertNotNull(acct0);
assertEquals(DualNodeTest.REMOTE, acct0.getBranch());
log.debug("Contents after refreshing in remote: " + TestingUtil.printCache(localCache));
// Double check with a brand new session, in case the other session
// for some reason bypassed the 2nd level cache
AccountDAO dao0A = new AccountDAO(useJta, localFactory);
Account acct0A = dao0A.getAccount(id);
assertNotNull(acct0A);
assertEquals(DualNodeTest.REMOTE, acct0A.getBranch());
log.debug("Contents after creating a new session: " + TestingUtil.printCache(localCache));
}
use of org.infinispan.manager.CacheContainer in project hibernate-orm by hibernate.
the class NaturalIdInvalidationTest method testAll.
@Test
public void testAll() throws Exception {
log.info("*** testAll()");
// Bind a listener to the "local" cache
// Our region factory makes its CacheManager available to us
CacheContainer localManager = ClusterAwareRegionFactory.getCacheManager(DualNodeTest.LOCAL);
Cache localNaturalIdCache = localManager.getCache(Citizen.class.getName() + "##NaturalId");
MyListener localListener = new MyListener("local");
localNaturalIdCache.addListener(localListener);
// Bind a listener to the "remote" cache
CacheContainer remoteManager = ClusterAwareRegionFactory.getCacheManager(DualNodeTest.REMOTE);
Cache remoteNaturalIdCache = remoteManager.getCache(Citizen.class.getName() + "##NaturalId");
MyListener remoteListener = new MyListener("remote");
remoteNaturalIdCache.addListener(remoteListener);
SessionFactory localFactory = sessionFactory();
SessionFactory remoteFactory = secondNodeEnvironment().getSessionFactory();
try {
assertTrue(remoteListener.isEmpty());
assertTrue(localListener.isEmpty());
CountDownLatch remoteUpdateLatch = expectAfterUpdate(remoteNaturalIdCache.getAdvancedCache(), 2);
saveSomeCitizens(localFactory);
assertTrue(remoteUpdateLatch.await(2, TimeUnit.SECONDS));
assertTrue(remoteListener.isEmpty());
assertTrue(localListener.isEmpty());
log.debug("Find node 0");
// This actually brings the collection into the cache
getCitizenWithCriteria(localFactory);
// Now the collection is in the cache so, the 2nd "get"
// should read everything from the cache
log.debug("Find(2) node 0");
localListener.clear();
getCitizenWithCriteria(localFactory);
// Check the read came from the cache
log.debug("Check cache 0");
assertLoadedFromCache(localListener, "1234");
log.debug("Find node 1");
// This actually brings the collection into the cache since invalidation is in use
getCitizenWithCriteria(remoteFactory);
// Now the collection is in the cache so, the 2nd "get"
// should read everything from the cache
log.debug("Find(2) node 1");
remoteListener.clear();
getCitizenWithCriteria(remoteFactory);
// Check the read came from the cache
log.debug("Check cache 1");
assertLoadedFromCache(remoteListener, "1234");
// Modify customer in remote
remoteListener.clear();
CountDownLatch localUpdate = expectEvict(localNaturalIdCache.getAdvancedCache(), 1);
deleteCitizenWithCriteria(remoteFactory);
assertTrue(localUpdate.await(2, TimeUnit.SECONDS));
Set localKeys = localNaturalIdCache.keySet();
assertEquals(1, localKeys.size());
// Only key left is the one for the citizen *not* in France
localKeys.toString().contains("000");
} catch (Exception e) {
log.error("Error", e);
throw e;
} finally {
withTxSession(localFactory, s -> {
s.createQuery("delete NaturalIdOnManyToOne").executeUpdate();
s.createQuery("delete Citizen").executeUpdate();
s.createQuery("delete State").executeUpdate();
});
}
}
Aggregations