Search in sources :

Example 6 with SoftLock

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));
}
Also used : TestSynchronization(org.hibernate.test.cache.infinispan.util.TestSynchronization) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Example 7 with 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);
    }
}
Also used : Serializable(java.io.Serializable) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) HibernateException(org.hibernate.HibernateException) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Example 8 with SoftLock

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));
}
Also used : TestSynchronization(org.hibernate.test.cache.infinispan.util.TestSynchronization) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Example 9 with 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();
    }
}
Also used : SoftLock(org.hibernate.cache.spi.access.SoftLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 10 with SoftLock

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();
    }
}
Also used : SoftLock(org.hibernate.cache.spi.access.SoftLock)

Aggregations

SoftLock (org.hibernate.cache.spi.access.SoftLock)16 Session (org.hibernate.Session)3 Transaction (org.hibernate.Transaction)3 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)3 TestSynchronization (org.hibernate.test.cache.infinispan.util.TestSynchronization)3 Serializable (java.io.Serializable)2 CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Test (org.junit.Test)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Predicate (java.util.function.Predicate)1