use of org.neo4j.ogm.exception.OptimisticLockingException in project neo4j-ogm by neo4j.
the class RelationshipEntityOptimisticLockingTest method optimisticLockingExceptionShouldRollbackDefaultTransaction.
@Test
public void optimisticLockingExceptionShouldRollbackDefaultTransaction() {
User michael = new User("Michael");
User oliver = new User("Oliver");
FriendOf friendOf = michael.addFriend(oliver);
session.save(michael);
friendOf.setSince(new Date());
session.save(friendOf);
michael.setName("Michael Updated");
oliver.setName("Oliver Updated");
FriendOf wrongVersion = new FriendOf(michael, oliver);
wrongVersion.setId(friendOf.getId());
wrongVersion.setVersion(0L);
Date updatedSince = new Date();
wrongVersion.setSince(updatedSince);
try {
session.save(wrongVersion, 0);
fail("Expected OptimisticLockingException");
} catch (OptimisticLockingException ex) {
session.clear();
Collection<User> users = session.loadAll(User.class);
assertThat(users).extracting(User::getName).containsOnly("Michael", "Oliver");
}
}
use of org.neo4j.ogm.exception.OptimisticLockingException in project neo4j-ogm by neo4j.
the class SessionCacheOptimisticLockingTest method shouldLoadNewNodeVersionInSessionAfterFailureToDelete.
/**
* Same case as {@link #shouldLoadNewNodeVersionInSessionAfterFailureToSave()}, only the operation is `delete`
*/
@Test
public void shouldLoadNewNodeVersionInSessionAfterFailureToDelete() {
Session session1 = sessionFactory.openSession();
User frantisek = new User("Frantisek");
session1.save(frantisek);
Session session2 = sessionFactory.openSession();
User updated = new User("Frantisek The Ugly");
updated.setId(frantisek.getId());
updated.setVersion(0L);
session2.save(updated);
try {
session1.delete(frantisek);
fail("Should have thrown OptimisticLockingException");
} catch (OptimisticLockingException e) {
// failed update should remove the instance from Session, on reload we should get the updated instance
User loaded = session1.load(User.class, frantisek.getId());
assertThat(loaded.getName()).isEqualTo("Frantisek The Ugly");
// after reload we should be able to delete
session1.delete(loaded);
loaded = session1.load(User.class, frantisek.getId());
assertThat(loaded).isNull();
}
}
use of org.neo4j.ogm.exception.OptimisticLockingException in project neo4j-ogm by neo4j.
the class SessionCacheOptimisticLockingTest method lockingWithExternalIdsShouldWorkInDifferentSessions.
@Test
public void lockingWithExternalIdsShouldWorkInDifferentSessions() throws InterruptedException {
final String cypherTemplate = "MATCH (n:VersionedEntityWithExternalId) WHERE n.name = 'Michael' RETURN n";
VersionedEntityWithExternalId emp = new VersionedEntityWithExternalId();
emp.setName("Michael");
session.save(emp);
CountDownLatch t1latch = new CountDownLatch(1);
CountDownLatch t2latch = new CountDownLatch(1);
CountDownLatch outer = new CountDownLatch(2);
AtomicBoolean gotException = new AtomicBoolean(false);
new Thread(() -> {
try {
Session session1 = sessionFactory.openSession();
VersionedEntityWithExternalId t1emp = session1.queryForObject(VersionedEntityWithExternalId.class, cypherTemplate, Collections.emptyMap());
t1emp.setSomething("OGM needs some real change here to trigger updates. " + ThreadLocalRandom.current().nextLong());
t2latch.countDown();
try {
t1latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// This is necessary. It opens a new session that is not aware of the thing with external id as in
// it doesn't know that it has already been assigned an internal id as well.
session1 = sessionFactory.openSession();
session1.save(t1emp);
} catch (OptimisticLockingException e) {
gotException.set(true);
} finally {
outer.countDown();
}
}).start();
new Thread(() -> {
try {
try {
t2latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
Session session2 = sessionFactory.openSession();
VersionedEntityWithExternalId t2emp = session2.queryForObject(VersionedEntityWithExternalId.class, cypherTemplate, Collections.emptyMap());
t2emp.setSomething("OGM needs some real change here to trigger updates. " + ThreadLocalRandom.current().nextLong());
session2.save(t2emp);
t1latch.countDown();
} catch (OptimisticLockingException e) {
gotException.set(true);
} finally {
outer.countDown();
}
}).start();
outer.await();
assertThat(gotException.get()).isTrue();
}
use of org.neo4j.ogm.exception.OptimisticLockingException in project neo4j-ogm by neo4j.
the class SessionCacheOptimisticLockingTest method shouldLoadNewRelationshipVersionInSessionAfterFailureToDeleteBySave.
@Test
public void shouldLoadNewRelationshipVersionInSessionAfterFailureToDeleteBySave() {
Session session1 = sessionFactory.openSession();
User alice = new User("Alice");
User bob = new User("Bob");
FriendOf friendOf = alice.addFriend(bob);
friendOf.setDescription("a-b");
session1.save(friendOf);
Session session2 = sessionFactory.openSession();
FriendOf updated = new FriendOf(alice, bob);
updated.setDescription("updated session 2");
updated.setId(friendOf.getId());
updated.setVersion(0L);
session2.save(updated, 0);
try {
// session1 has old version of friendOf in session, should fail
alice.clearFriends();
bob.clearFriends();
session1.save(alice);
fail("Should have thrown OptimisticLockingException");
} catch (OptimisticLockingException e) {
// failed update should remove the instance from Session, on reload we should get the updated instance
FriendOf loaded = session1.load(FriendOf.class, friendOf.getId());
assertThat(loaded.getDescription()).isEqualTo("updated session 2");
session1.delete(loaded);
loaded = session1.load(FriendOf.class, friendOf.getId());
assertThat(loaded).isNull();
}
}
use of org.neo4j.ogm.exception.OptimisticLockingException in project neo4j-ogm by neo4j.
the class SessionCacheOptimisticLockingTest method shouldLoadNewRelationshipVersionInSessionAfterFailureToSave.
@Test
public void shouldLoadNewRelationshipVersionInSessionAfterFailureToSave() {
Session session1 = sessionFactory.openSession();
User michael = new User("Michael");
User oliver = new User("Oliver");
FriendOf friendOf = michael.addFriend(oliver);
friendOf.setDescription("m-o");
session1.save(friendOf);
Session session2 = sessionFactory.openSession();
FriendOf updated = new FriendOf(michael, oliver);
updated.setDescription("updated session 2");
updated.setId(friendOf.getId());
updated.setVersion(0L);
session2.save(updated, 0);
try {
friendOf.setDescription("updated session 1");
session1.save(friendOf, 0);
fail("Should have thrown OptimisticLockingException");
} catch (OptimisticLockingException e) {
// failed update should remove the instance from Session, on reload we should get the updated instance
FriendOf loaded = session1.load(FriendOf.class, friendOf.getId());
assertThat(loaded.getDescription()).isEqualTo("updated session 2");
}
}
Aggregations