List of usage examples for org.hibernate StaleObjectStateException getEntityName
public String getEntityName()
From source file:com.eucalyptus.simpleworkflow.SimpleWorkflowService.java
License:Open Source License
public com.eucalyptus.simpleworkflow.common.model.ActivityTask pollForActivityTask( final PollForActivityTaskRequest request) throws SimpleWorkflowException { final Context ctx = Contexts.lookup(); final UserFullName userFullName = ctx.getUserFullName(); final AccountFullName accountFullName = userFullName.asAccountFullName(); final Predicate<? super ActivityTask> accessible = SimpleWorkflowMetadatas.filteringFor(ActivityTask.class) .byPrivileges().buildPredicate(); final String domain = request.getDomain(); final String taskList = request.getTaskList().getName(); final Callable<com.eucalyptus.simpleworkflow.common.model.ActivityTask> taskCallable = new Callable<com.eucalyptus.simpleworkflow.common.model.ActivityTask>() { @Override/*from w w w.j a v a 2s . c o m*/ public com.eucalyptus.simpleworkflow.common.model.ActivityTask call() throws Exception { com.eucalyptus.simpleworkflow.common.model.ActivityTask activityTask = null; final List<ActivityTask> pending = activityTasks.listByExample( ActivityTask.examplePending(accountFullName, domain, taskList), accessible, Functions.<ActivityTask>identity()); Collections.sort(pending, Ordering.natural().onResultOf(AbstractPersistentSupport.creation())); for (final ActivityTask pendingTask : pending) { if (activityTask != null) break; boolean retry = true; while (retry) try (final WorkflowLock lock = WorkflowLock.lock(accountFullName, pendingTask.getDomainUuid(), pendingTask.getWorkflowRunId())) { retry = false; activityTask = activityTasks.updateByExample(pendingTask, accountFullName, pendingTask.getDisplayName(), new Function<ActivityTask, com.eucalyptus.simpleworkflow.common.model.ActivityTask>() { @Nullable @Override public com.eucalyptus.simpleworkflow.common.model.ActivityTask apply( final ActivityTask activityTask) { if (activityTask.getState() == ActivityTask.State.Pending) { final WorkflowExecution workflowExecution = activityTask .getWorkflowExecution(); final Long startedId = workflowExecution.addHistoryEvent( WorkflowHistoryEvent.create(workflowExecution, new ActivityTaskStartedEventAttributes() .withIdentity(request.getIdentity()) .withScheduledEventId(activityTask .getScheduledEventId()))); activityTask.setState(ActivityTask.State.Active); activityTask.setStartedEventId(startedId); return new com.eucalyptus.simpleworkflow.common.model.ActivityTask() .withStartedEventId(startedId) .withInput(activityTask.getInput()) .withTaskToken(taskTokenManager.encryptTaskToken( new TaskToken(accountFullName.getAccountNumber(), workflowExecution.getDomain() .getNaturalId(), workflowExecution.getDisplayName(), activityTask.getScheduledEventId(), startedId, System.currentTimeMillis(), System.currentTimeMillis()))) .withActivityId(activityTask.getDisplayName()) .withActivityType( new com.eucalyptus.simpleworkflow.common.model.ActivityType() .withName(activityTask.getActivityType()) .withVersion( activityTask.getActivityVersion())) .withWorkflowExecution( new com.eucalyptus.simpleworkflow.common.model.WorkflowExecution() .withRunId( workflowExecution.getDisplayName()) .withWorkflowId( workflowExecution.getWorkflowId())); } return null; } }); } catch (SwfMetadataException e) { logger.info("Activity task for domain " + domain + ", list " + taskList + " not found"); } catch (Exception e) { final StaleObjectStateException stale = Exceptions.findCause(e, StaleObjectStateException.class); if (stale != null) try { Entities.evictCache(Class.forName(stale.getEntityName())); } catch (ClassNotFoundException ce) { /* eviction failure */ } if (PersistenceExceptions.isStaleUpdate(e)) { logger.info("Activity task for domain " + domain + ", list " + taskList + " already taken"); } else if (PersistenceExceptions.isLockError(e)) { logger.info("Activity task for domain " + domain + ", list " + taskList + " locking error, will retry."); Thread.sleep(10); retry = true; } else { logger.error( "Error taking activity task for domain " + domain + ", list " + taskList, e); } } } return activityTask; } }; try { handleTaskPolling(accountFullName, domain, "activity", taskList, request.getCorrelationId(), new com.eucalyptus.simpleworkflow.common.model.ActivityTask(), taskCallable); } catch (Exception e) { throw handleException(e); } return null; }
From source file:com.eucalyptus.simpleworkflow.SimpleWorkflowService.java
License:Open Source License
public DecisionTask pollForDecisionTask(final PollForDecisionTaskRequest request) throws SimpleWorkflowException { final Context ctx = Contexts.lookup(); final UserFullName userFullName = ctx.getUserFullName(); final AccountFullName accountFullName = userFullName.asAccountFullName(); final Predicate<? super WorkflowExecution> accessible = SimpleWorkflowMetadatas .filteringFor(WorkflowExecution.class).byPrivileges().buildPredicate(); final String domain = request.getDomain(); final String taskList = request.getTaskList().getName(); final Callable<DecisionTask> taskCallable = new Callable<DecisionTask>() { @Override/*from w ww . java 2 s .c o m*/ public DecisionTask call() throws Exception { final List<WorkflowExecution> pending = workflowExecutions.listByExample( WorkflowExecution.exampleWithPendingDecision(accountFullName, domain, taskList), accessible, Functions.<WorkflowExecution>identity()); Collections.sort(pending, Ordering.natural().onResultOf(AbstractPersistentSupport.creation())); DecisionTask decisionTask = null; for (final WorkflowExecution execution : pending) { if (decisionTask != null) break; boolean retry = true; while (retry) try (final WorkflowLock lock = WorkflowLock.lock(accountFullName, execution.getDomainUuid(), execution.getDisplayName())) { retry = false; decisionTask = workflowExecutions.updateByExample( WorkflowExecution.exampleWithUniqueName(accountFullName, execution.getDomainName(), execution.getDisplayName()), accountFullName, execution.getDisplayName(), new Function<WorkflowExecution, DecisionTask>() { @Nullable @Override public DecisionTask apply(final WorkflowExecution workflowExecution) { if (workflowExecution.getDecisionStatus() == Pending) { final List<WorkflowHistoryEvent> events = workflowExecution .getWorkflowHistory(); final List<WorkflowHistoryEvent> reverseEvents = Lists .reverse(events); final WorkflowHistoryEvent scheduled = Iterables.find(reverseEvents, CollectionUtils.propertyPredicate("DecisionTaskScheduled", EVENT_TYPE)); final Optional<WorkflowHistoryEvent> previousStarted = Iterables .tryFind(reverseEvents, CollectionUtils.propertyPredicate( "DecisionTaskStarted", EVENT_TYPE)); workflowExecution.setDecisionStatus(Active); workflowExecution.setDecisionTimestamp(new Date()); final WorkflowHistoryEvent started = WorkflowHistoryEvent.create( workflowExecution, new DecisionTaskStartedEventAttributes() .withIdentity(request.getIdentity()) .withScheduledEventId(scheduled.getEventId())); workflowExecution.addHistoryEvent(started); return new DecisionTask().withWorkflowExecution( new com.eucalyptus.simpleworkflow.common.model.WorkflowExecution() .withWorkflowId(workflowExecution.getWorkflowId()) .withRunId(workflowExecution.getDisplayName())) .withWorkflowType( new com.eucalyptus.simpleworkflow.common.model.WorkflowType() .withName(workflowExecution .getWorkflowType().getDisplayName()) .withVersion( workflowExecution.getWorkflowType() .getWorkflowVersion())) .withTaskToken(taskTokenManager.encryptTaskToken( new TaskToken(accountFullName.getAccountNumber(), workflowExecution.getDomain() .getNaturalId(), workflowExecution.getDisplayName(), scheduled.getEventId(), started.getEventId(), System.currentTimeMillis(), System.currentTimeMillis()))) //TODO:STEVE: token expiry date .withStartedEventId(started.getEventId()) .withPreviousStartedEventId(previousStarted.transform( WorkflowExecutions.WorkflowHistoryEventLongFunctions.EVENT_ID) .or(0L)) .withEvents(Collections2.transform( Objects.firstNonNull(request.isReverseOrder(), Boolean.FALSE) ? reverseEvents : events, TypeMappers.lookup(WorkflowHistoryEvent.class, HistoryEvent.class))); } return null; } }); } catch (Exception e) { final StaleObjectStateException stale = Exceptions.findCause(e, StaleObjectStateException.class); if (stale != null) try { Entities.evictCache(Class.forName(stale.getEntityName())); } catch (ClassNotFoundException ce) { /* eviction failure */ } if (PersistenceExceptions.isStaleUpdate(e)) { logger.info("Decision task for workflow " + execution.getDisplayName() + " already taken."); } else if (PersistenceExceptions.isLockError(e)) { logger.info("Decision task for workflow " + execution.getDisplayName() + " locking error, will retry."); Thread.sleep(10); retry = true; } else { logger.error( "Error taking decision task for workflow " + execution.getDisplayName(), e); } } } return decisionTask; } }; try { handleTaskPolling(accountFullName, domain, "decision", taskList, request.getCorrelationId(), new DecisionTask(), taskCallable); } catch (Exception e) { throw handleException(e); } return null; }
From source file:gr.interamerican.bo2.impl.open.hibernate.AbstractHibernatePersistenceUtility.java
License:Open Source License
/** * Logs a StaleObjectStateException.//from ww w. j a va2 s. c o m * * @param he * StaleObjectStateException. This object contains the class name * and the id of the object on which the check failed. * @param o * The object on which the worker operation is performed */ void logStaleObjectException(StaleObjectStateException he, Object o) { logHibernateException(he); specialLogHibernateException(he.getEntityName(), he.getIdentifier(), o); }
From source file:org.granite.hibernate.HibernateOptimisticLockException.java
License:Open Source License
public static void rethrowOptimisticLockException(Session session, StaleObjectStateException sose) { Serializable identifier = sose.getIdentifier(); if (identifier != null) { try {/* w w w. j a v a 2s.c o m*/ Object entity = session.load(sose.getEntityName(), identifier); if (entity instanceof Serializable) { //avoid some user errors regarding boundary crossing throw new HibernateOptimisticLockException(null, sose, entity); } } catch (ObjectNotFoundException onfe) { // Ignored, StaleStateException will be rethrown } } }
From source file:org.seamless.gwt.server.HibernateRemoteServiceServlet.java
License:Open Source License
@Override protected String onBeforeResponseSerialized(Object result) { if (!getCurrentSession().getTransaction().isActive()) return null; try {/*from w w w . j a v a2s .co m*/ // Commit and cleanup log.fine("Committing the database transaction"); getCurrentSession().getTransaction().commit(); } catch (RuntimeException ex) { // Rollback only log.fine("Runtime exception occurred, considering transaction rollback: " + ex); try { if (getCurrentSession().getTransaction().isActive()) { log.fine("Trying to rollback database transaction after exception"); getCurrentSession().getTransaction().rollback(); log.fine("Transaction rolled back"); } } catch (Throwable rbEx) { log.log(Level.SEVERE, "Could not rollback transaction after exception!", rbEx); } // Note that the exception you marshall to the client has to be // serializable/cross-compiled (in your 'shared' or 'client' package) // and it has to implement com.google.gwt.user.client.rpc.IsSerializable // to pass the serialization policy security. if (ex instanceof StaleObjectStateException) { StaleObjectStateException sosEx = (StaleObjectStateException) ex; log.fine("Stale object state detected, serializing message to client for: " + sosEx); StringBuilder sb = new StringBuilder(); sb.append("Concurrent modification error, "); sb.append("simultaneous modification of '").append(sosEx.getEntityName()).append("'"); sb.append(" with identifier: ").append(sosEx.getIdentifier()); ValidationException serializableException = new ValidationException(sb.toString()); try { return encodeResponseForFailure(null, serializableException); } catch (SerializationException e) { log.fine("Can't serialize concurrent modification error message: " + e); throw ex; } } if (ex instanceof ConstraintViolationException) { ConstraintViolationException cvEx = (ConstraintViolationException) ex; log.fine("Integrity constraint violation detected, serializing message to client for: " + cvEx); StringBuilder sb = new StringBuilder(); sb.append("Violation of database integrity, "); sb.append("error code: ").append(cvEx.getErrorCode()); ValidationException serializableException = new ValidationException(sb.toString()); try { return encodeResponseForFailure(null, serializableException); } catch (SerializationException e) { log.fine("Can't serialize integrity rule violation message: " + e); throw ex; } } else { throw ex; } } return null; }
From source file:org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException.java
License:Apache License
public HibernateOptimisticLockingFailureException(StaleObjectStateException ex) { super(ex.getEntityName(), ex.getIdentifier(), ex); }