use of org.hibernate.cache.spi.Region in project hibernate-orm by hibernate.
the class ConcurrentStatisticsImpl method getSecondLevelCacheStatistics.
/**
* Second level cache statistics per region
*
* @param regionName region name
*
* @return SecondLevelCacheStatistics
*/
public ConcurrentSecondLevelCacheStatisticsImpl getSecondLevelCacheStatistics(String regionName) {
ConcurrentSecondLevelCacheStatisticsImpl stat = secondLevelCacheStatistics.get(regionName);
if (stat == null) {
if (sessionFactory == null) {
return null;
}
final EntityRegionAccessStrategy entityRegionAccess = sessionFactory.getCache().getEntityRegionAccess(regionName);
final CollectionRegionAccessStrategy collectionRegionAccess = sessionFactory.getCache().getCollectionRegionAccess(regionName);
if (entityRegionAccess == null && collectionRegionAccess == null) {
final Region region = sessionFactory.getCache().getQueryCache(regionName).getRegion();
if (region == null) {
throw new IllegalArgumentException("Could not resolve region name [" + regionName + "]");
}
stat = new ConcurrentSecondLevelCacheStatisticsImpl(region, null, null);
} else {
final Region region = entityRegionAccess != null ? entityRegionAccess.getRegion() : collectionRegionAccess.getRegion();
stat = new ConcurrentSecondLevelCacheStatisticsImpl(region, entityRegionAccess, collectionRegionAccess);
}
ConcurrentSecondLevelCacheStatisticsImpl previous;
if ((previous = secondLevelCacheStatistics.putIfAbsent(regionName, stat)) != null) {
stat = previous;
}
}
return stat;
}
use of org.hibernate.cache.spi.Region in project hibernate-orm by hibernate.
the class QueryRegionImplTest method testQueryUpdate.
@Test
public void testQueryUpdate() throws Exception {
withQueryRegion((sessionFactory, region) -> {
ExceptionHolder holder = new ExceptionHolder();
CyclicBarrier barrier = new CyclicBarrier(2);
withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
Thread updater = new Thread() {
@Override
public void run() {
try {
withSession(sessionFactory, (session) -> {
assertEquals(VALUE1, region.get(session, KEY));
region.put(session, KEY, VALUE2);
assertEquals(VALUE2, region.get(session, KEY));
barrier.await(5, TimeUnit.SECONDS);
barrier.await(5, TimeUnit.SECONDS);
region.put(session, KEY, VALUE3);
assertEquals(VALUE3, region.get(session, KEY));
barrier.await(5, TimeUnit.SECONDS);
barrier.await(5, TimeUnit.SECONDS);
});
} catch (AssertionFailedError e) {
holder.addAssertionFailure(e);
barrier.reset();
} catch (Exception e) {
holder.addException(e);
barrier.reset();
}
}
};
Thread reader = new Thread() {
@Override
public void run() {
try {
withSession(sessionFactory, (session) -> {
assertEquals(VALUE1, region.get(session, KEY));
barrier.await(5, TimeUnit.SECONDS);
assertEquals(VALUE1, region.get(session, KEY));
barrier.await(5, TimeUnit.SECONDS);
barrier.await(5, TimeUnit.SECONDS);
assertEquals(VALUE1, region.get(session, KEY));
barrier.await(5, TimeUnit.SECONDS);
});
} catch (AssertionFailedError e) {
holder.addAssertionFailure(e);
barrier.reset();
} catch (Exception e) {
holder.addException(e);
barrier.reset();
}
}
};
updater.start();
reader.start();
updater.join();
reader.join();
holder.checkExceptions();
assertEquals(VALUE3, callWithSession(sessionFactory, session -> region.get(session, KEY)));
});
}
use of org.hibernate.cache.spi.Region in project hibernate-orm by hibernate.
the class QueryRegionImplTest method testGetDoesNotBlockPut.
@Test
public void testGetDoesNotBlockPut() throws Exception {
withQueryRegion((sessionFactory, region) -> {
withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
final AdvancedCache cache = ((QueryResultsRegionImpl) region).getCache();
final CountDownLatch blockerLatch = 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() {
GetBlocker blocker = new GetBlocker(blockerLatch, KEY);
try {
cache.addListener(blocker);
withSession(sessionFactory, session -> region.get(session, KEY));
} catch (Exception e) {
holder.addException(e);
} finally {
cache.removeListener(blocker);
}
}
};
Thread writer = new Thread() {
@Override
public void run() {
try {
writerLatch.await();
withSession(sessionFactory, session -> region.put(session, KEY, VALUE2));
} catch (Exception e) {
holder.addException(e);
} finally {
completionLatch.countDown();
}
}
};
reader.setDaemon(true);
writer.setDaemon(true);
boolean unblocked = false;
try {
reader.start();
writer.start();
assertFalse("Reader is blocking", completionLatch.await(100, TimeUnit.MILLISECONDS));
// Start the writer
writerLatch.countDown();
assertTrue("Writer finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS));
blockerLatch.countDown();
unblocked = true;
if (IsolationLevel.REPEATABLE_READ.equals(cache.getCacheConfiguration().locking().isolationLevel())) {
assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
} else {
assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
}
holder.checkExceptions();
} finally {
if (!unblocked) {
blockerLatch.countDown();
}
}
});
}
use of org.hibernate.cache.spi.Region in project hibernate-orm by hibernate.
the class QueryRegionImplTest method testPutDuringPut.
@Test
@TestForIssue(jiraKey = "HHH-7898")
public void testPutDuringPut() throws Exception {
withQueryRegion((sessionFactory, region) -> {
withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
final AdvancedCache cache = ((QueryResultsRegionImpl) region).getCache();
CountDownLatch blockerLatch = new CountDownLatch(1);
CountDownLatch triggerLatch = new CountDownLatch(1);
ExceptionHolder holder = new ExceptionHolder();
Thread blocking = new Thread() {
@Override
public void run() {
PutBlocker blocker = null;
try {
blocker = new PutBlocker(blockerLatch, triggerLatch, KEY);
cache.addListener(blocker);
withSession(sessionFactory, session -> region.put(session, KEY, VALUE2));
} catch (Exception e) {
holder.addException(e);
} finally {
if (blocker != null) {
cache.removeListener(blocker);
}
if (triggerLatch.getCount() > 0) {
triggerLatch.countDown();
}
}
}
};
Thread blocked = new Thread() {
@Override
public void run() {
try {
triggerLatch.await();
// this should silently fail
withSession(sessionFactory, session -> region.put(session, KEY, VALUE3));
} catch (Exception e) {
holder.addException(e);
}
}
};
blocking.setName("blocking-thread");
blocking.start();
blocked.setName("blocked-thread");
blocked.start();
blocked.join();
blockerLatch.countDown();
blocking.join();
holder.checkExceptions();
assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
});
}
use of org.hibernate.cache.spi.Region 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)));
});
}
Aggregations