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();
}
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));
}
}
}
}
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);
}
}
}
}
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;
}
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);
}
}
Aggregations