use of org.hibernate.cache.spi.support.DomainDataRegionTemplate in project hibernate-orm by hibernate.
the class HibernateCacheTest method testQueryCacheInvalidation.
@Test
public void testQueryCacheInvalidation() throws Exception {
Session s = sessionFactory().openSession();
Transaction t = s.beginTransaction();
Item i = new Item();
i.setName("widget");
i.setDescription("A really top-quality, full-featured widget.");
s.persist(i);
t.commit();
s.close();
CacheRegionStatistics slcs = sessionFactory().getStatistics().getDomainDataRegionStatistics(Item.class.getName());
assertThat(slcs.getPutCount(), equalTo(1L));
assertTrue(sessionFactory().getCache().containsEntity(Item.class, i.getId()));
s = sessionFactory().openSession();
t = s.beginTransaction();
i = (Item) s.get(Item.class, i.getId());
assertThat(slcs.getHitCount(), equalTo(1L));
assertThat(slcs.getMissCount(), equalTo(0L));
i.setDescription("A bog standard item");
t.commit();
s.close();
assertThat(slcs.getPutCount(), equalTo(2L));
assertTrue(sessionFactory().getCache().containsEntity(Item.class, i.getId()));
final DomainDataRegionTemplate region = (DomainDataRegionTemplate) sessionFactory().getMetamodel().entityPersister(Item.class).getCacheAccessStrategy().getRegion();
final Object fromCache = region.getCacheStorageAccess().getFromCache(region.getEffectiveKeysFactory().createEntityKey(i.getId(), sessionFactory().getMetamodel().entityPersister(Item.class), sessionFactory(), null), (SharedSessionContractImplementor) s);
assertNotNull(fromCache);
ExtraAssertions.assertTyping(AbstractReadWriteAccess.Item.class, fromCache);
// assertThat( (String) map.get( "description" ), equalTo( "A bog standard item" ) );
// assertThat( (String) map.get( "name" ), equalTo( "widget" ) );
// cleanup
s = sessionFactory().openSession();
t = s.beginTransaction();
s.delete(i);
t.commit();
s.close();
}
use of org.hibernate.cache.spi.support.DomainDataRegionTemplate in project hibernate-orm by hibernate.
the class DomainDataRegionTest method testBasicUsage.
@Test
public void testBasicUsage() {
final Region region = sessionFactory().getCache().getRegion(TestHelper.entityRegionNames[0]);
final DomainDataRegionTemplate domainDataRegion = assertTyping(DomainDataRegionTemplate.class, region);
// see if we can get access to all of the access objects we think should be defined in this region
final EntityDataAccess itemAccess = domainDataRegion.getEntityDataAccess(sessionFactory().getMetamodel().entityPersister(Item.class).getNavigableRole());
assertThat(itemAccess.getAccessType(), equalTo(AccessType.READ_WRITE));
assertThat(sessionFactory().getMetamodel().entityPersister(VersionedItem.class).getCacheAccessStrategy().getAccessType(), equalTo(AccessType.READ_WRITE));
assertThat(sessionFactory().getMetamodel().entityPersister(Event.class).getCacheAccessStrategy().getAccessType(), equalTo(AccessType.READ_WRITE));
assertThat(sessionFactory().getMetamodel().collectionPersister(Event.class.getName() + ".participants").getCacheAccessStrategy().getAccessType(), equalTo(AccessType.READ_WRITE));
}
use of org.hibernate.cache.spi.support.DomainDataRegionTemplate in project hibernate-orm by hibernate.
the class HibernateCacheTest method testStaleWritesLeaveCacheConsistent.
// @Test
// public void testEmptySecondLevelCacheEntry() throws Exception {
// sessionFactory().getCache().evictEntityRegion( Item.class.getName() );
// Statistics stats = sessionFactory().getStatistics();
// stats.clear();
// CacheRegionStatistics statistics = stats.getDomainDataRegionStatistics( Item.class.getName() );
// Map cacheEntries = statistics.getEntries();
// assertThat( cacheEntries.size(), equalTo( 0 ) );
// }
@Test
public void testStaleWritesLeaveCacheConsistent() {
Session s = sessionFactory().openSession();
Transaction txn = s.beginTransaction();
VersionedItem item = new VersionedItem();
item.setName("steve");
item.setDescription("steve's item");
s.save(item);
txn.commit();
s.close();
Long initialVersion = item.getVersion();
// manually revert the version property
item.setVersion(item.getVersion() - 1);
try {
s = sessionFactory().openSession();
txn = s.beginTransaction();
s.update(item);
txn.commit();
s.close();
fail("expected stale write to fail");
} catch (Throwable expected) {
// expected behavior here
if (txn != null) {
try {
txn.rollback();
} catch (Throwable ignore) {
}
}
} finally {
if (s != null && s.isOpen()) {
try {
s.close();
} catch (Throwable ignore) {
}
}
}
// // check the version value in the cache...
// CacheRegionStatistics slcs = sessionFactory().getStatistics()
// .getDomainDataRegionStatistics( VersionedItem.class.getName() );
// assertNotNull(slcs);
// final Map entries = slcs.getEntries();
// Object entry = entries.get( item.getId() );
// Long cachedVersionValue;
// if ( entry instanceof SoftLock ) {
// //FIXME don't know what to test here
// //cachedVersionValue = new Long( ( (ReadWriteCache.Lock) entry).getUnlockTimestamp() );
// }
// else {
// cachedVersionValue = (Long) ( (Map) entry ).get( "_version" );
// assertThat( initialVersion, equalTo( cachedVersionValue ) );
// }
final DomainDataRegionTemplate region = (DomainDataRegionTemplate) sessionFactory().getMetamodel().entityPersister(Item.class).getCacheAccessStrategy().getRegion();
final Object fromCache = region.getCacheStorageAccess().getFromCache(region.getEffectiveKeysFactory().createEntityKey(item.getId(), sessionFactory().getMetamodel().entityPersister(Item.class), sessionFactory(), null), (SharedSessionContractImplementor) s);
assertTrue(fromCache == null || fromCache instanceof SoftLock);
// cleanup
s = sessionFactory().openSession();
txn = s.beginTransaction();
item = s.load(VersionedItem.class, item.getId());
s.delete(item);
txn.commit();
s.close();
}
use of org.hibernate.cache.spi.support.DomainDataRegionTemplate in project hibernate-orm by hibernate.
the class InsertedDataTest method testInsertWithRefreshThenRollback.
@Test
public void testInsertWithRefreshThenRollback() {
sessionFactory().getCache().evictEntityRegions();
sessionFactory().getStatistics().clear();
inTransaction(sessionFactory, s -> {
CacheableItem item = new CacheableItem("data");
s.save(item);
s.flush();
s.refresh(item);
s.getTransaction().markRollbackOnly();
});
inTransaction(sessionFactory, s -> {
final DomainDataRegionTemplate region = (DomainDataRegionTemplate) sessionFactory().getCache().getRegion("item");
final Object fromCache = region.getCacheStorageAccess().getFromCache(region.getEffectiveKeysFactory().createEntityKey(1L, sessionFactory().getMetamodel().entityPersister(CacheableItem.class), sessionFactory(), null), s);
assertNotNull(fromCache);
ExtraAssertions.assertTyping(SoftLock.class, fromCache);
});
inTransaction(sessionFactory, s -> {
CacheableItem item = s.get(CacheableItem.class, 1L);
assertNull("it should be null", item);
});
}
Aggregations