Search in sources :

Example 1 with AfterTransactionCompletionProcess

use of org.hibernate.action.spi.AfterTransactionCompletionProcess in project hibernate-orm by hibernate.

the class AuditProcessManager method get.

public AuditProcess get(EventSource session) {
    final Transaction transaction = session.accessTransaction();
    AuditProcess auditProcess = auditProcesses.get(transaction);
    if (auditProcess == null) {
        // No worries about registering a transaction twice - a transaction is single thread
        auditProcess = new AuditProcess(revisionInfoGenerator, session);
        auditProcesses.put(transaction, auditProcess);
        session.getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {

            public void doBeforeTransactionCompletion(SessionImplementor session) {
                final AuditProcess process = auditProcesses.get(transaction);
                if (process != null) {
                    process.doBeforeTransactionCompletion(session);
                }
            }
        });
        session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

            public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                auditProcesses.remove(transaction);
            }
        });
    }
    return auditProcess;
}
Also used : BeforeTransactionCompletionProcess(org.hibernate.action.spi.BeforeTransactionCompletionProcess) Transaction(org.hibernate.Transaction) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 2 with AfterTransactionCompletionProcess

use of org.hibernate.action.spi.AfterTransactionCompletionProcess in project hibernate-orm by hibernate.

the class DefaultRefreshEventListener method onRefresh.

/**
	 * Handle the given refresh event.
	 *
	 * @param event The refresh event to be handled.
	 */
public void onRefresh(RefreshEvent event, Map refreshedAlready) {
    final EventSource source = event.getSession();
    boolean isTransient;
    if (event.getEntityName() != null) {
        isTransient = !source.contains(event.getEntityName(), event.getObject());
    } else {
        isTransient = !source.contains(event.getObject());
    }
    if (source.getPersistenceContext().reassociateIfUninitializedProxy(event.getObject())) {
        if (isTransient) {
            source.setReadOnly(event.getObject(), source.isDefaultReadOnly());
        }
        return;
    }
    final Object object = source.getPersistenceContext().unproxyAndReassociate(event.getObject());
    if (refreshedAlready.containsKey(object)) {
        LOG.trace("Already refreshed");
        return;
    }
    final EntityEntry e = source.getPersistenceContext().getEntry(object);
    final EntityPersister persister;
    final Serializable id;
    if (e == null) {
        persister = source.getEntityPersister(event.getEntityName(), object);
        //refresh() does not pass an entityName
        id = persister.getIdentifier(object, event.getSession());
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Refreshing transient {0}", MessageHelper.infoString(persister, id, source.getFactory()));
        }
        final EntityKey key = source.generateEntityKey(id, persister);
        if (source.getPersistenceContext().getEntry(key) != null) {
            throw new PersistentObjectException("attempted to refresh transient instance when persistent instance was already associated with the Session: " + MessageHelper.infoString(persister, id, source.getFactory()));
        }
    } else {
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Refreshing ", MessageHelper.infoString(e.getPersister(), e.getId(), source.getFactory()));
        }
        if (!e.isExistsInDatabase()) {
            throw new UnresolvableObjectException(e.getId(), "this instance does not yet exist as a row in the database");
        }
        persister = e.getPersister();
        id = e.getId();
    }
    // cascade the refresh prior to refreshing this entity
    refreshedAlready.put(object, object);
    Cascade.cascade(CascadingActions.REFRESH, CascadePoint.BEFORE_REFRESH, source, persister, object, refreshedAlready);
    if (e != null) {
        final EntityKey key = source.generateEntityKey(id, persister);
        source.getPersistenceContext().removeEntity(key);
        if (persister.hasCollections()) {
            new EvictVisitor(source).process(object, persister);
        }
    }
    if (persister.hasCache()) {
        Object previousVersion = null;
        if (persister.isVersionPropertyGenerated()) {
            // we need to grab the version value from the entity, otherwise
            // we have issues with generated-version entities that may have
            // multiple actions queued during the same flush
            previousVersion = persister.getVersion(object);
        }
        final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        final Object ck = cache.generateCacheKey(id, persister, source.getFactory(), source.getTenantIdentifier());
        final SoftLock lock = cache.lockItem(source, ck, previousVersion);
        source.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

            @Override
            public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                cache.unlockItem(session, ck, lock);
            }
        });
        cache.remove(source, ck);
    }
    evictCachedCollections(persister, id, source);
    String previousFetchProfile = source.getLoadQueryInfluencers().getInternalFetchProfile();
    source.getLoadQueryInfluencers().setInternalFetchProfile("refresh");
    Object result = persister.load(id, object, event.getLockOptions(), source);
    // If it was transient, then set it to the default for the source.
    if (result != null) {
        if (!persister.isMutable()) {
            // this is probably redundant; it should already be read-only
            source.setReadOnly(result, true);
        } else {
            source.setReadOnly(result, (e == null ? source.isDefaultReadOnly() : e.isReadOnly()));
        }
    }
    source.getLoadQueryInfluencers().setInternalFetchProfile(previousFetchProfile);
    UnresolvableObjectException.throwIfNull(result, id, persister.getEntityName());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistentObjectException(org.hibernate.PersistentObjectException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Example 3 with AfterTransactionCompletionProcess

use of org.hibernate.action.spi.AfterTransactionCompletionProcess in project hibernate-orm by hibernate.

the class DefaultRefreshEventListener method evictCachedCollections.

private void evictCachedCollections(Type[] types, Serializable id, EventSource source) throws HibernateException {
    for (Type type : types) {
        if (type.isCollectionType()) {
            CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister(((CollectionType) type).getRole());
            if (collectionPersister.hasCache()) {
                final CollectionRegionAccessStrategy cache = collectionPersister.getCacheAccessStrategy();
                final Object ck = cache.generateCacheKey(id, collectionPersister, source.getFactory(), source.getTenantIdentifier());
                final SoftLock lock = cache.lockItem(source, ck, null);
                source.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

                    @Override
                    public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                        cache.unlockItem(session, ck, lock);
                    }
                });
                cache.remove(source, ck);
            }
        } else if (type.isComponentType()) {
            CompositeType actype = (CompositeType) type;
            evictCachedCollections(actype.getSubtypes(), id, source);
        }
    }
}
Also used : CollectionType(org.hibernate.type.CollectionType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) SoftLock(org.hibernate.cache.spi.access.SoftLock) CompositeType(org.hibernate.type.CompositeType)

Example 4 with AfterTransactionCompletionProcess

use of org.hibernate.action.spi.AfterTransactionCompletionProcess in project hibernate-orm by hibernate.

the class CollectionCacheInvalidator method evict.

private void evict(Serializable id, CollectionPersister collectionPersister, EventSource session) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Evict CollectionRegion " + collectionPersister.getRole() + " for id " + id);
    }
    AfterTransactionCompletionProcess afterTransactionProcess = new CollectionEvictCacheAction(collectionPersister, null, id, session).lockCache();
    session.getActionQueue().registerProcess(afterTransactionProcess);
}
Also used : AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess)

Aggregations

AfterTransactionCompletionProcess (org.hibernate.action.spi.AfterTransactionCompletionProcess)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)3 SoftLock (org.hibernate.cache.spi.access.SoftLock)2 Serializable (java.io.Serializable)1 PersistentObjectException (org.hibernate.PersistentObjectException)1 Transaction (org.hibernate.Transaction)1 UnresolvableObjectException (org.hibernate.UnresolvableObjectException)1 BeforeTransactionCompletionProcess (org.hibernate.action.spi.BeforeTransactionCompletionProcess)1 CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)1 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 EventSource (org.hibernate.event.spi.EventSource)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 CollectionType (org.hibernate.type.CollectionType)1 CompositeType (org.hibernate.type.CompositeType)1 Type (org.hibernate.type.Type)1