use of org.hibernate.cache.spi.access.SoftLock in project hibernate-orm by hibernate.
the class EntityRegionAccessStrategyTest method doUpdate.
@Override
protected void doUpdate(EntityRegionAccessStrategy strategy, SharedSessionContractImplementor session, Object key, Object value, Object version) throws javax.transaction.RollbackException, javax.transaction.SystemException {
SoftLock softLock = strategy.lockItem(session, key, null);
strategy.update(session, key, value, null, null);
session.getTransactionCoordinator().getLocalSynchronizations().registerSynchronization(new TestSynchronization.AfterUpdate(strategy, session, key, value, version, softLock));
}
use of org.hibernate.cache.spi.access.SoftLock in project hibernate-orm by hibernate.
the class CollectionCacheInvalidator method evictCache.
private void evictCache(Object entity, EntityPersister persister, EventSource session, Object[] oldState) {
try {
SessionFactoryImplementor factory = persister.getFactory();
Set<String> collectionRoles = factory.getMetamodel().getCollectionRolesByEntityParticipant(persister.getEntityName());
if (collectionRoles == null || collectionRoles.isEmpty()) {
return;
}
for (String role : collectionRoles) {
final CollectionPersister collectionPersister = factory.getMetamodel().collectionPersister(role);
if (!collectionPersister.hasCache()) {
// ignore collection if no caching is used
continue;
}
// this is the property this OneToMany relation is mapped by
String mappedBy = collectionPersister.getMappedByProperty();
if (!collectionPersister.isManyToMany() && mappedBy != null && !mappedBy.isEmpty()) {
int i = persister.getEntityMetamodel().getPropertyIndex(mappedBy);
Serializable oldId = null;
if (oldState != null) {
// in case of updating an entity we perhaps have to decache 2 entity collections, this is the
// old one
oldId = getIdentifier(session, oldState[i]);
}
Object ref = persister.getPropertyValue(entity, i);
Serializable id = getIdentifier(session, ref);
// only evict if the related entity has changed
if ((id != null && !id.equals(oldId)) || (oldId != null && !oldId.equals(id))) {
if (id != null) {
evict(id, collectionPersister, session);
}
if (oldId != null) {
evict(oldId, collectionPersister, session);
}
}
} else {
LOG.debug("Evict CollectionRegion " + role);
final SoftLock softLock = collectionPersister.getCacheAccessStrategy().lockRegion();
session.getActionQueue().registerProcess((success, session1) -> {
collectionPersister.getCacheAccessStrategy().unlockRegion(softLock);
});
}
}
} catch (Exception e) {
if (PROPAGATE_EXCEPTION) {
throw new IllegalStateException(e);
}
// don't let decaching influence other logic
LOG.error("", e);
}
}
use of org.hibernate.cache.spi.access.SoftLock in project hibernate-orm by hibernate.
the class AbstractRegionAccessStrategyTest method doRemove.
protected void doRemove(S strategy, SharedSessionContractImplementor session, Object key) throws SystemException, RollbackException {
SoftLock softLock = strategy.lockItem(session, key, null);
strategy.remove(session, key);
session.getTransactionCoordinator().getLocalSynchronizations().registerSynchronization(new TestSynchronization.UnlockItem(strategy, session, key, softLock));
}
use of org.hibernate.cache.spi.access.SoftLock in project hibernate-orm by hibernate.
the class AbstractReadWriteAccessStrategy method lockItem.
/**
* Soft-lock a cache item.
*/
@Override
public final SoftLock lockItem(SharedSessionContractImplementor session, Object key, Object version) throws CacheException {
try {
LOG.debugf("locking key[%s] in region[%s]", key, getInternalRegion().getName());
writeLock.lock();
Lockable item = (Lockable) getInternalRegion().get(session, key);
long timeout = getInternalRegion().nextTimestamp() + getInternalRegion().getTimeout();
final Lock lock = (item == null) ? new Lock(timeout, uuid, nextLockId(), version) : item.lock(timeout, uuid, nextLockId());
getInternalRegion().put(session, key, lock);
return lock;
} finally {
writeLock.unlock();
}
}
use of org.hibernate.cache.spi.access.SoftLock in project hibernate-orm by hibernate.
the class ReadWriteEntityRegionAccessStrategy method afterUpdate.
@Override
public boolean afterUpdate(SharedSessionContractImplementor session, Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) throws CacheException {
try {
writeLock.lock();
Lockable item = (Lockable) region.get(session, key);
if (item != null && item.isUnlockable(lock)) {
Lock lockItem = (Lock) item;
if (lockItem.wasLockedConcurrently()) {
decrementLock(session, key, lockItem);
return false;
} else {
region.put(session, key, new Item(value, currentVersion, region.nextTimestamp()));
return true;
}
} else {
handleLockExpiry(session, key, item);
return false;
}
} finally {
writeLock.unlock();
}
}
Aggregations