Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 1 file changed
  • 0 comments
  • 2 contributors
Mar 11, 2013
Arvid Huss arvidhuss Introduced lock functionality to ConcurrentModificationCheckConcern t…
…o prevent simultaneous updates of the same entity.
4da4e22
Mar 15, 2013
Paul Merlin eskatos Merge pull request #11 from arvidhuss/develop
Introduced lock functionality to ConcurrentModificationCheckConcern
Reviewed by Rickard and Niclas.
fbdec84
40 core/spi/src/main/java/org/qi4j/spi/entitystore/ConcurrentModificationCheckConcern.java
@@ -14,8 +14,6 @@
14 14
15 15 package org.qi4j.spi.entitystore;
16 16
17   -import java.util.ArrayList;
18   -import java.util.List;
19 17 import org.qi4j.api.Qi4j;
20 18 import org.qi4j.api.concern.ConcernOf;
21 19 import org.qi4j.api.entity.EntityDescriptor;
@@ -26,6 +24,10 @@
26 24 import org.qi4j.api.usecase.Usecase;
27 25 import org.qi4j.spi.entity.EntityState;
28 26
  27 +import java.util.ArrayList;
  28 +import java.util.List;
  29 +import java.util.concurrent.locks.ReentrantReadWriteLock;
  30 +
29 31 /**
30 32 * Concern that helps EntityStores do concurrent modification checks.
31 33 * <p/>
@@ -61,6 +63,8 @@ public EntityStoreUnitOfWork newUnitOfWork( Usecase usecase, Module module, long
61 63
62 64 private List<EntityState> loaded = new ArrayList<EntityState>();
63 65
  66 + private ReentrantReadWriteLock lock = new ReentrantReadWriteLock( );
  67 +
64 68 public ConcurrentCheckingEntityStoreUnitOfWork( EntityStoreUnitOfWork uow,
65 69 EntityStateVersions versions,
66 70 Module module,
@@ -96,6 +100,8 @@ public EntityState newEntityState( EntityReference anIdentity, EntityDescriptor
96 100 public StateCommitter applyChanges()
97 101 throws EntityStoreException
98 102 {
  103 + lock.writeLock().lock();
  104 +
99 105 versions.checkForConcurrentModification( loaded, module, currentTime );
100 106
101 107 final StateCommitter committer = uow.applyChanges();
@@ -107,6 +113,8 @@ public void commit()
107 113 {
108 114 committer.commit();
109 115 versions.forgetVersions( loaded );
  116 +
  117 + lock.writeLock().unlock();
110 118 }
111 119
112 120 @Override
@@ -114,6 +122,8 @@ public void cancel()
114 122 {
115 123 committer.cancel();
116 124 versions.forgetVersions( loaded );
  125 +
  126 + lock.writeLock().unlock();
117 127 }
118 128 };
119 129 }
@@ -127,7 +137,15 @@ public void discard()
127 137 }
128 138 finally
129 139 {
130   - versions.forgetVersions( loaded );
  140 + lock.writeLock().lock();
  141 +
  142 + try
  143 + {
  144 + versions.forgetVersions( loaded );
  145 + } finally
  146 + {
  147 + lock.writeLock().unlock();
  148 + }
131 149 }
132 150 }
133 151
@@ -135,10 +153,18 @@ public void discard()
135 153 public EntityState entityStateOf( EntityReference anIdentity )
136 154 throws EntityStoreException, EntityNotFoundException
137 155 {
138   - EntityState entityState = uow.entityStateOf( anIdentity );
139   - versions.rememberVersion( entityState.identity(), entityState.version() );
140   - loaded.add( entityState );
141   - return entityState;
  156 + lock.readLock().lock();
  157 +
  158 + try
  159 + {
  160 + EntityState entityState = uow.entityStateOf( anIdentity );
  161 + versions.rememberVersion( entityState.identity(), entityState.version() );
  162 + loaded.add( entityState );
  163 + return entityState;
  164 + } finally
  165 + {
  166 + lock.readLock().unlock();
  167 + }
142 168 }
143 169 }
144 170 }

No commit comments for this range

Something went wrong with that request. Please try again.