cancel
Showing results for 
Search instead for 
Did you mean: 

Apache Chemistry session flush or commit

juniorbl
Champ in-the-making
Champ in-the-making
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.
3 REPLIES 3

andy
Champ on-the-rise
Champ on-the-rise
Hi

What version of Alfresco are you using?
Are you using SOLR? If so - you are probably seeing eventual consitency??

Can you answer your question without using query?

There is always the chance someone could have created a doc with some name before you do - if there is more then one user/process.

Andy

juniorbl
Champ in-the-making
Champ in-the-making
Hi,

I'm using Alfresco 4d and Solr, it's my local machine, only I have access. I tried to retrieve the same document using "session.getObjectByPath(path)" and it worked, it's a little weird, sometimes the query works, sometimes it doesn't.

Thank you.

andy
Champ on-the-rise
Champ on-the-rise
Hi

This sounds like SOLR eventual consistency.
If you debug and wait it is there ….

Andy