Search in sources :

Example 1 with ObjectDeletedException

use of org.hibernate.ObjectDeletedException in project hibernate-orm by hibernate.

the class SessionImpl method getCurrentLockMode.

@Override
public LockMode getCurrentLockMode(Object object) throws HibernateException {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        throw new NullPointerException("null object passed to getCurrentLockMode()");
    }
    if (object instanceof HibernateProxy) {
        object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation(this);
        if (object == null) {
            return LockMode.NONE;
        }
    }
    EntityEntry e = persistenceContext.getEntry(object);
    if (e == null) {
        throw new TransientObjectException("Given object not associated with the session");
    }
    if (e.getStatus() != Status.MANAGED) {
        throw new ObjectDeletedException("The given object was deleted", e.getId(), e.getPersister().getEntityName());
    }
    return e.getLockMode();
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry) ObjectDeletedException(org.hibernate.ObjectDeletedException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 2 with ObjectDeletedException

use of org.hibernate.ObjectDeletedException in project hibernate-orm by hibernate.

the class DefaultMergeEventListener method onMerge.

/**
 * Handle the given merge event.
 *
 * @param event The merge event to be handled.
 *
 * @throws HibernateException
 */
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
    final MergeContext copyCache = (MergeContext) copiedAlready;
    final EventSource source = event.getSession();
    final Object original = event.getOriginal();
    if (original != null) {
        final Object entity;
        if (original instanceof HibernateProxy) {
            LazyInitializer li = ((HibernateProxy) original).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                LOG.trace("Ignoring uninitialized proxy");
                event.setResult(source.load(li.getEntityName(), li.getIdentifier()));
                // EARLY EXIT!
                return;
            } else {
                entity = li.getImplementation();
            }
        } else {
            entity = original;
        }
        if (copyCache.containsKey(entity) && (copyCache.isOperatedOn(entity))) {
            LOG.trace("Already in merge process");
            event.setResult(entity);
        } else {
            if (copyCache.containsKey(entity)) {
                LOG.trace("Already in copyCache; setting in merge process");
                copyCache.setOperatedOn(entity, true);
            }
            event.setEntity(entity);
            EntityState entityState = null;
            // Check the persistence context for an entry relating to this
            // entity to be merged...
            EntityEntry entry = source.getPersistenceContext().getEntry(entity);
            if (entry == null) {
                EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
                Serializable id = persister.getIdentifier(entity, source);
                if (id != null) {
                    final EntityKey key = source.generateEntityKey(id, persister);
                    final Object managedEntity = source.getPersistenceContext().getEntity(key);
                    entry = source.getPersistenceContext().getEntry(managedEntity);
                    if (entry != null) {
                        // we have specialized case of a detached entity from the
                        // perspective of the merge operation.  Specifically, we
                        // have an incoming entity instance which has a corresponding
                        // entry in the current persistence context, but registered
                        // under a different entity instance
                        entityState = EntityState.DETACHED;
                    }
                }
            }
            if (entityState == null) {
                entityState = getEntityState(entity, event.getEntityName(), entry, source);
            }
            switch(entityState) {
                case DETACHED:
                    entityIsDetached(event, copyCache);
                    break;
                case TRANSIENT:
                    entityIsTransient(event, copyCache);
                    break;
                case PERSISTENT:
                    entityIsPersistent(event, copyCache);
                    break;
                default:
                    // DELETED
                    throw new ObjectDeletedException("deleted instance passed to merge", null, getLoggableName(event.getEntityName(), entity));
            }
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) ObjectDeletedException(org.hibernate.ObjectDeletedException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 3 with ObjectDeletedException

use of org.hibernate.ObjectDeletedException in project hibernate-orm by hibernate.

the class AbstractLockUpgradeEventListener method upgradeLock.

/**
 * Performs a pessimistic lock upgrade on a given entity, if needed.
 *
 * @param object The entity for which to upgrade the lock.
 * @param entry The entity's EntityEntry instance.
 * @param lockOptions contains the requested lock mode.
 * @param source The session which is the source of the event being processed.
 */
protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) {
    LockMode requestedLockMode = lockOptions.getLockMode();
    if (requestedLockMode.greaterThan(entry.getLockMode())) {
        if (entry.getStatus() != Status.MANAGED) {
            throw new ObjectDeletedException("attempted to lock a deleted instance", entry.getId(), entry.getPersister().getEntityName());
        }
        final EntityPersister persister = entry.getPersister();
        if (log.isTraceEnabled()) {
            log.tracev("Locking {0} in mode: {1}", MessageHelper.infoString(persister, entry.getId(), source.getFactory()), requestedLockMode);
        }
        final boolean cachingEnabled = persister.canWriteToCache();
        SoftLock lock = null;
        Object ck = null;
        try {
            if (cachingEnabled) {
                EntityDataAccess cache = persister.getCacheAccessStrategy();
                ck = cache.generateCacheKey(entry.getId(), persister, source.getFactory(), source.getTenantIdentifier());
                lock = cache.lockItem(source, ck, entry.getVersion());
            }
            if (persister.isVersioned() && requestedLockMode == LockMode.FORCE) {
                // todo : should we check the current isolation mode explicitly?
                Object nextVersion = persister.forceVersionIncrement(entry.getId(), entry.getVersion(), source);
                entry.forceLocked(object, nextVersion);
            } else {
                persister.lock(entry.getId(), entry.getVersion(), object, lockOptions, source);
            }
            entry.setLockMode(requestedLockMode);
        } finally {
            // so release the soft lock
            if (cachingEnabled) {
                persister.getCacheAccessStrategy().unlockItem(source, ck, lock);
            }
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ObjectDeletedException(org.hibernate.ObjectDeletedException) LockMode(org.hibernate.LockMode) SoftLock(org.hibernate.cache.spi.access.SoftLock) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Example 4 with ObjectDeletedException

use of org.hibernate.ObjectDeletedException in project OpenOLAT by OpenOLAT.

the class VideoTranscodingJob method updateStatus.

/**
 * Update the transcoding object in database.
 *
 * @param proc The transcoding process
 * @param videoTranscoding The video transcoding object
 * @return false if the transcoding object was deleted
 */
private boolean updateStatus(Process proc, VideoTranscoding videoTranscoding) {
    VideoManager videoManager = CoreSpringFactory.getImpl(VideoManager.class);
    try (InputStream stdout = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(stdout);
        BufferedReader br = new BufferedReader(isr)) {
        String line = null;
        while ((line = br.readLine()) != null) {
            // Parse the percentage. Logline looks like this:
            // Encoding: task 1 of 1, 85.90 % (307.59 fps, avg 330.35 fps, ETA 00h00m05s)
            int start = line.indexOf(",");
            if (start != -1) {
                line = line.substring(start);
                int end = line.indexOf(".");
                if (end != -1 && end < 5) {
                    String percent = line.substring(2, end);
                    log.debug("Output: " + percent);
                    // update version file for UI
                    try {
                        videoTranscoding.setStatus(Integer.parseInt(percent));
                        videoTranscoding = videoManager.updateVideoTranscoding(videoTranscoding);
                        DBFactory.getInstance().commitAndCloseSession();
                    } catch (ObjectDeletedException e) {
                        // deleted by other process
                        proc.destroy();
                        br.close();
                        return false;
                    }
                }
            }
        }
    } catch (IOException e) {
        log.error("", e);
    }
    return true;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ObjectDeletedException(org.hibernate.ObjectDeletedException) IOException(java.io.IOException) VideoManager(org.olat.modules.video.VideoManager)

Example 5 with ObjectDeletedException

use of org.hibernate.ObjectDeletedException in project OpenOLAT by OpenOLAT.

the class OLATUpgrade_7_1_0 method migrateNotificationPublishers.

private void migrateNotificationPublishers(UpgradeManager upgradeManager, UpgradeHistoryData uhd) {
    if (!uhd.getBooleanDataValue(TASK_CHECK_NOTIFICATIONS)) {
        log.audit("+-----------------------------------------------------------------------------+");
        log.audit("+... Check the businesspath for the publishers (notifications)             ...+");
        log.audit("+-----------------------------------------------------------------------------+");
        if (!portletRepositoryTeacherEnabled && !portletRepositoryStudentEnabled) {
            log.audit("**** Repository portlets disabled: don't need to check publishers. ****");
            uhd.setBooleanDataValue(TASK_CHECK_NOTIFICATIONS, true);
            upgradeManager.setUpgradesHistory(uhd, VERSION);
            return;
        }
        int counter = 0;
        NotificationsManager notificationMgr = NotificationsManager.getInstance();
        List<Publisher> allPublishers = notificationMgr.getAllPublisher();
        if (log.isDebug())
            log.info("Found " + allPublishers.size() + " publishers to check.");
        for (Publisher publisher : allPublishers) {
            if (publisher != null && StringHelper.containsNonWhitespace(publisher.getBusinessPath()) && (publisher.getBusinessPath().startsWith("[Identity") || publisher.getBusinessPath().startsWith("ROOT[Identity"))) {
                try {
                    String businessPath = publisher.getBusinessPath();
                    int startIndex = businessPath.indexOf("[Identity");
                    int stopIndex = businessPath.indexOf("]", startIndex);
                    int wide = stopIndex - startIndex;
                    if (wide > 30) {
                        // Identity:326394598 cannot be too wide
                        continue;
                    } else if (stopIndex + 1 >= businessPath.length()) {
                        // only identity
                        continue;
                    }
                    String correctPath = businessPath.substring(stopIndex + 1);
                    publisher.setBusinessPath(correctPath);
                    DBFactory.getInstance().updateObject(publisher);
                } catch (ObjectDeletedException e) {
                    log.warn("Publisher was already deleted, no update possible! Publisher key: " + publisher.getKey());
                } catch (Exception e) {
                    log.warn("Publisher was already deleted, no update possible! Publisher key: " + publisher.getKey());
                }
                counter++;
            }
            if (counter > 0 && counter % 100 == 0) {
                log.audit("Another 100 publishers done");
                DBFactory.getInstance().intermediateCommit();
            }
        }
        DBFactory.getInstance().intermediateCommit();
        log.audit("**** Checked " + counter + " publishers. ****");
        uhd.setBooleanDataValue(TASK_CHECK_NOTIFICATIONS, true);
        upgradeManager.setUpgradesHistory(uhd, VERSION);
    }
}
Also used : NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) ObjectDeletedException(org.hibernate.ObjectDeletedException) Publisher(org.olat.core.commons.services.notifications.Publisher) ObjectDeletedException(org.hibernate.ObjectDeletedException)

Aggregations

ObjectDeletedException (org.hibernate.ObjectDeletedException)12 NotificationsManager (org.olat.core.commons.services.notifications.NotificationsManager)4 Publisher (org.olat.core.commons.services.notifications.Publisher)4 EntityEntry (org.hibernate.engine.spi.EntityEntry)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 MappingException (org.hibernate.MappingException)2 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)2 LazyInitializer (org.hibernate.proxy.LazyInitializer)2 VideoManager (org.olat.modules.video.VideoManager)2 Serializable (java.io.Serializable)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 LockMode (org.hibernate.LockMode)1 LockOptions (org.hibernate.LockOptions)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)1 PersistentObjectException (org.hibernate.PersistentObjectException)1