Permalink
Browse files

Merge pull request #11 from arvidhuss/develop

Introduced lock functionality to ConcurrentModificationCheckConcern
Reviewed by Rickard and Niclas.
  • Loading branch information...
2 parents 9b47a8c + 4da4e22 commit fbdec8483999e0620a3e9eca0e89de2e213f639d @eskatos eskatos committed Mar 15, 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 fbdec84

Please sign in to comment.