DocumentAlreadyExistsException when creating node
mikkus70 Jan 29, 2013 12:16 PMHi,
I'm using ModeShape 3.1.0.Final embedded in an application (repository cache backed by a JdbcStringCacheStore) deployed in JBoss 5.1.0.GA. Most of the JCR functionality works as advertised, but I'm facing issues doing a rather simple operation, adding a node to a newly added parent node. I'm using Jackrabbit's OCM for entity mapping, without problems for the most part except in this case.
The operation I'm trying to perform is the following:
if (getInstance().getPath() != null && getInstance().getPath().contains("/")) { String parent = getInstance().getPath().substring(0, getInstance().getPath().lastIndexOf("/")); JcrUtils.createPath(getContentManager().getSession(), parent); } getContentManager().insert(getInstance()); getContentManager().save();
JcrUtils.createPath() simply creates the parent nodes if they don't exist, starting from session.getRootNode() and calling node.addNode(pathSegment, "nt:unstructured") if a path segment does not exist. Finally I call the insert() method of OCM to perform the mapping and create the new node. ModeShape throws a DocumentAlreadyExistsException when calling save(). If I remove the createPath() call, ModeShape complains that the parent node does not exist (therefore I can exclude that the path already exists). Variable "parent" maps to "/comments/wiki/en/main", whereas the OCM is mapped into "/comments/wiki/en/main/comments".
This is the exception's stacktrace:
Caused by: org.apache.jackrabbit.ocm.exception.ObjectContentManagerException: Cannot persist current session changes.; nested exception is javax.jcr.InvalidItemStateException: This session tried to create node '/comments/wiki/en/main/comments' (with key '20ee2e990a8834167ee7cf-8841-4202-886e-74ce7c0b5c42'), but a node with that key already exists. at org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl.save(ObjectContentManagerImpl.java:1078) at com.sample.core.dms.session.AbstractDmsHome.persist(AbstractDmsHome.java:127) at com.sample.core.dms.session.ForumMessageHome.persist(ForumMessageHome.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56) at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.bpm.BusinessProcessInterceptor.aroundInvoke(BusinessProcessInterceptor.java:51) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:97) at org.jboss.seam.util.Work.workInTransaction(Work.java:47) at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.EventInterceptor.aroundInvoke(EventInterceptor.java:52) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107) at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185) at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103) at com.sample.core.dms.session.ForumMessageHome_$$_javassist_seam_36.persist(ForumMessageHome_$$_javassist_seam_36.java) at com.sample.core.dms.session.ForumThreadHome.persist(ForumThreadHome.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56) at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.bpm.BusinessProcessInterceptor.aroundInvoke(BusinessProcessInterceptor.java:51) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:97) at org.jboss.seam.util.Work.workInTransaction(Work.java:47) at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.EventInterceptor.aroundInvoke(EventInterceptor.java:52) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107) at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185) at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103) at com.sample.core.dms.session.ForumThreadHome_$$_javassist_seam_34.persist(ForumThreadHome_$$_javassist_seam_34.java) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335) at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:348) at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58) at org.jboss.el.parser.AstValue.invoke(AstValue.java:96) at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 55 more Caused by: javax.jcr.InvalidItemStateException: This session tried to create node '/comments/wiki/en/main/comments'' (with key '20ee2e990a8834167ee7cf-8841-4202-886e-74ce7c0b5c42'), but a node with that key already exists. at org.modeshape.jcr.JcrSession.save(JcrSession.java:948) at org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl.save(ObjectContentManagerImpl.java:1070) ... 119 more Caused by: org.modeshape.jcr.cache.DocumentAlreadyExistsException: 20ee2e990a8834167ee7cf-8841-4202-886e-74ce7c0b5c42 at org.modeshape.jcr.cache.document.WritableSessionCache.persistChanges(WritableSessionCache.java:1036) at org.modeshape.jcr.cache.document.WritableSessionCache.save(WritableSessionCache.java:517) at org.modeshape.jcr.JcrSession.save(JcrSession.java:935) ... 120 more
Is is possible to understand the reason of this behaviour? I use this mechanism elsewhere and have no problems.
Thanks in advance.