AnsweredAssumed Answered

Apache Chemistry session flush or commit

Question asked by juniorbl on May 22, 2012
Latest reply on May 24, 2012 by andy
Hello everyone,

I'm using Chemistry to add or update documents in Alfresco, something like this:
private AlfrescoDocument addDocument(SourceObject sourceObject, Folder parentFolder, Map<String, Object> properties) {
      properties.put(PropertyIds.OBJECT_TYPE_ID, properties.get(PropertyIds.OBJECT_TYPE_ID));
      properties.put(PropertyIds.NAME, "myDocument");
      ContentStream contentStream = createContenStream(sourceObject);
      return (AlfrescoDocument) parentFolder.createDocument(properties, contentStream, VersioningState.MAJOR);
The update method uses checkin and checkout to add a new version when the document already exists:
private void updateDocument(SourceObject sourceObject, AlfrescoDocument doc) {
      ContentStream contentStream = createContenStream(sourceObject);
      ObjectId pwcId = doc.checkOut();
      Document pwcDocument = (Document) session.getObject(pwcId);
      boolean majorVersion = true;
      pwcDocument.checkIn(majorVersion, null, contentStream, null);
And the update is called only if the following query return something:
public static AlfrescoDocument searchAlfrescoDocument(Session session, String name, String basePath) {
      AlfrescoDocument alfrescoDocument = null;
      String query = "SELECT * FROM cmis:document WHERE cmis:name = '" + name + "'";
      ItemIterable<QueryResult> queryResult = session.query(query, true);
      for (QueryResult item : queryResult) {
          String itemObjectId = item.getPropertyByQueryName("cmis:objectId").getFirstValue().toString();
         Document resultDocument = (Document) session.getObject(session.createObjectId(itemObjectId));
          if (resultDocument.getPaths().get(0).startsWith(basePath)) {
             if (alfrescoDocument == null) {
                alfrescoDocument = (AlfrescoDocument)resultDocument;
      return alfrescoDocument;
It works fine but sometimes the search doesn't find a recently added document and throws a "CmisConstraintException: Conflict" because it tries to add a document that is already there. If I debug the code, stop right before the search and wait for a few seconds, the search finds the document and the update method is called. Is there a way to flush or commit the session or do something to avoid this kind of error?

Thank you.