DATAGRAPH-413: ConcurrentModificationException occurs when trying to persist entity after modification outside of transaction

This commit is contained in:
Nicki Watt
2013-12-09 23:32:59 +00:00
parent b83f4c1d11
commit 112465ca33
2 changed files with 30 additions and 0 deletions

View File

@@ -178,6 +178,27 @@ public class ModificationOutsideOfTransactionTests extends EntityTestBase {
}
}
@Test
public void testSetPropertyOutsideTransactionCanBePersistedThereafter()
{
Person p = persistedPerson( "Michael", 35 );
p.setAge( 25 );
assertEquals(25, p.getAge());
try (Transaction tx = neo4jTemplate.getGraphDatabase().beginTx()) {
assertEquals( 35, nodeFor( p ).getProperty("age") );
tx.success();
}
p.persist();
try (Transaction tx = neo4jTemplate.getGraphDatabase().beginTx()) {
assertEquals( 25, nodeFor( p ).getProperty("age") );
tx.success();
}
}
@Test
public void shouldWorkWithUninitializedCollectionFieldWithoutUnderlyingState() {
Group group = new Group();

View File

@@ -159,6 +159,15 @@ public class DetachedEntityState<STATE> implements EntityState<STATE> {
Object valueFromDb = null;
if (template.transactionIsRunning()) {
valueFromDb = unwrap(delegate.getValue(property, MappingPolicy.MAP_FIELD_DIRECT_POLICY));
} else {
// For Implicit Transactions, we need to create
// a tx to ensure we get the correct previous value
// otherwise the possibility of getting a concurrent
// modification exception when next persisting may occur
try (Transaction tx = template.getGraphDatabaseService().beginTx()) {
valueFromDb = unwrap(delegate.getValue(property, MappingPolicy.MAP_FIELD_DIRECT_POLICY));
tx.success();
}
}
addDirty(property, valueFromDb, true);
}