use of org.hibernate.SessionFactory 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();
});
}
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class QueryRegionImplTest method testPutDoesNotBlockGet.
@Test
public void testPutDoesNotBlockGet() throws Exception {
withQueryRegion((sessionFactory, region) -> {
withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
final CountDownLatch readerLatch = new CountDownLatch(1);
final CountDownLatch writerLatch = new CountDownLatch(1);
final CountDownLatch completionLatch = new CountDownLatch(1);
final ExceptionHolder holder = new ExceptionHolder();
Thread reader = new Thread() {
@Override
public void run() {
try {
assertNotEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
} catch (AssertionFailedError e) {
holder.addAssertionFailure(e);
} catch (Exception e) {
holder.addException(e);
} finally {
readerLatch.countDown();
}
}
};
Thread writer = new Thread() {
@Override
public void run() {
try {
withSession(sessionFactory, session -> {
region.put(session, KEY, VALUE2);
writerLatch.await();
});
} catch (Exception e) {
holder.addException(e);
} finally {
completionLatch.countDown();
}
}
};
reader.setDaemon(true);
writer.setDaemon(true);
writer.start();
assertFalse("Writer is blocking", completionLatch.await(100, TimeUnit.MILLISECONDS));
// Start the reader
reader.start();
assertTrue("Reader finished promptly", readerLatch.await(100, TimeUnit.MILLISECONDS));
writerLatch.countDown();
assertTrue("Reader finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS));
assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
});
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class DropSchemaDuringJtaTxnTest method testDrop.
@Test
public void testDrop() throws Exception {
final SessionFactory sessionFactory = buildSessionFactory();
sessionFactory.close();
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class HibernateUtil method getSessionFactory.
private SessionFactory getSessionFactory() {
if (sf == null) {
Bundle thisBundle = FrameworkUtil.getBundle(HibernateUtil.class);
// Could get this by wiring up OsgiTestBundleActivator as well.
BundleContext context = thisBundle.getBundleContext();
ServiceReference sr = context.getServiceReference(SessionFactory.class.getName());
sf = (SessionFactory) context.getService(sr);
}
return sf;
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class SessionFactoryRegistry method getSessionFactory.
public SessionFactory getSessionFactory(String uuid) {
LOG.debugf("Lookup: uid=%s", uuid);
final SessionFactory sessionFactory = sessionFactoryMap.get(uuid);
if (sessionFactory == null && LOG.isDebugEnabled()) {
LOG.debugf("Not found: %s", uuid);
LOG.debugf(sessionFactoryMap.toString());
}
return sessionFactory;
}
Aggregations