PersistenceException and InvalidItemStateException: OakState0001 exception in AEM 6.5.10
In my project, I have multiple event listeners, workflows, and Sling jobs that can modify the very same asset at the same time which sometimes causes commit conflicts. Obviously, different JCR sessions try to save different changes. I thought that refreshing the session before the actual .commit() call would help, but it didn't.
I need some explanation why the following code throws this exception
org.apache.sling.api.resource.PersistenceException: Unable to commit changes to session.
..
Caused by: javax.jcr.InvalidItemStateException: OakState0001: Unresolved conflicts in /content/dam...
ResourceResolver rr1 = ...get new service resource resolver
ResourceResolver rr2 = ...get new service resource resolver
Node node1 = rr1.getResource("/content/dam/.../asset.jpg/jcr:content/metadata").adaptTo(Node.class);
node1.setProperty("dc:title", "title 1");
Node node2 = rr2.getResource("/content/dam/.../asset.jpg/jcr:content/metadata").adaptTo(Node.class);
node2.setProperty("dc:title", "title 2");
rr1.commit();
rr2.refresh();
rr2.commit();