Skip to content

Commit

Permalink
improve(engine): OptimisticLockingListener type awareness
Browse files Browse the repository at this point in the history
Allow OptimisticLockingListener to select Entity types it wants to
handle.

related to #CAM-2805
  • Loading branch information
meyerdan committed Oct 2, 2014
1 parent a9d9043 commit 5a90785
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.camunda.bpm.engine.impl.Page;
import org.camunda.bpm.engine.impl.context.Context;
import org.camunda.bpm.engine.impl.db.DbEntity;
import org.camunda.bpm.engine.impl.db.entitymanager.OptimisticLockingListener;
import org.camunda.bpm.engine.impl.db.entitymanager.operation.DbEntityOperation;
import org.camunda.bpm.engine.impl.db.entitymanager.operation.DbOperation;
Expand Down Expand Up @@ -97,6 +98,10 @@ protected void lockJob(JobEntity job, String lockOwner, int lockTimeInMillis) {
job.setLockExpirationTime(gregorianCalendar.getTime());
}

public Class<? extends DbEntity> getEntityType() {
return JobEntity.class;
}

public void failedOperation(DbOperation operation) {
if (operation instanceof DbEntityOperation) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,19 @@ public void flush() {
}

protected void handleOptimisticLockingException(DbOperation dbOperation) {
boolean isHandled = false;

if(optimisticLockingListeners != null) {
for (OptimisticLockingListener optimisticLockingListener : optimisticLockingListeners) {
optimisticLockingListener.failedOperation(dbOperation);
if(optimisticLockingListener.getEntityType() == null
|| optimisticLockingListener.getEntityType().isAssignableFrom(dbOperation.getEntityType())) {
optimisticLockingListener.failedOperation(dbOperation);
isHandled = true;
}
}
} else {
}

if(!isHandled) {
throw new OptimisticLockingException("Could not execute "+dbOperation + ". Entity was updated by another transaction concurrently");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.camunda.bpm.engine.impl.db.entitymanager;

import org.camunda.bpm.engine.impl.db.DbEntity;
import org.camunda.bpm.engine.impl.db.entitymanager.operation.DbOperation;
import org.camunda.bpm.engine.impl.db.entitymanager.operation.DbOperationType;

Expand All @@ -25,6 +26,20 @@
*/
public interface OptimisticLockingListener {

/**
* The type of the entity for which this listener should be notified.
* If the implementation returns 'null', the listener is notified for all
* entity types.
*
* @return the entity type for which the listener should be notified.
*/
Class<? extends DbEntity> getEntityType();

/**
* Signifies that an operation failed due to optimistic locking.
*
* @param operation the failed operation.
*/
void failedOperation(DbOperation operation);

}

0 comments on commit 5a90785

Please sign in to comment.