Permalink
Browse files

Introduced lock functionality to ConcurrentModificationCheckConcern t…

…o prevent simultaneous updates of the same entity.
  • Loading branch information...
1 parent 5b513e0 commit 4da4e22133f687b439413e4a70e56f963a4cf2ff @arvidhuss arvidhuss committed Mar 11, 2013
@@ -14,8 +14,6 @@
package org.qi4j.spi.entitystore;
-import java.util.ArrayList;
-import java.util.List;
import org.qi4j.api.Qi4j;
import org.qi4j.api.concern.ConcernOf;
import org.qi4j.api.entity.EntityDescriptor;
@@ -26,6 +24,10 @@
import org.qi4j.api.usecase.Usecase;
import org.qi4j.spi.entity.EntityState;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
/**
* Concern that helps EntityStores do concurrent modification checks.
* <p/>
@@ -61,6 +63,8 @@ public EntityStoreUnitOfWork newUnitOfWork( Usecase usecase, Module module, long
private List<EntityState> loaded = new ArrayList<EntityState>();
+ private ReentrantReadWriteLock lock = new ReentrantReadWriteLock( );
+
public ConcurrentCheckingEntityStoreUnitOfWork( EntityStoreUnitOfWork uow,
EntityStateVersions versions,
Module module,
@@ -96,6 +100,8 @@ public EntityState newEntityState( EntityReference anIdentity, EntityDescriptor
public StateCommitter applyChanges()
throws EntityStoreException
{
+ lock.writeLock().lock();
+
versions.checkForConcurrentModification( loaded, module, currentTime );
final StateCommitter committer = uow.applyChanges();
@@ -107,13 +113,17 @@ public void commit()
{
committer.commit();
versions.forgetVersions( loaded );
+
+ lock.writeLock().unlock();
}
@Override
public void cancel()
{
committer.cancel();
versions.forgetVersions( loaded );
+
+ lock.writeLock().unlock();
}
};
}
@@ -127,18 +137,34 @@ public void discard()
}
finally
{
- versions.forgetVersions( loaded );
+ lock.writeLock().lock();
+
+ try
+ {
+ versions.forgetVersions( loaded );
+ } finally
+ {
+ lock.writeLock().unlock();
+ }
}
}
@Override
public EntityState entityStateOf( EntityReference anIdentity )
throws EntityStoreException, EntityNotFoundException
{
- EntityState entityState = uow.entityStateOf( anIdentity );
- versions.rememberVersion( entityState.identity(), entityState.version() );
- loaded.add( entityState );
- return entityState;
+ lock.readLock().lock();
+
+ try
+ {
+ EntityState entityState = uow.entityStateOf( anIdentity );
+ versions.rememberVersion( entityState.identity(), entityState.version() );
+ loaded.add( entityState );
+ return entityState;
+ } finally
+ {
+ lock.readLock().unlock();
+ }
}
}
}

0 comments on commit 4da4e22

Please sign in to comment.