Hi,
We are using code to check if a folder exists and if not, then create it. This is working fine and am able to generate a complete folder structure.
However, we are doing a conversion involving many concurrent requests to the API and if two come in with the same path at the exact same time, we are getting an exception.
org.alfresco.service.cmr.model.FileExistsException: 07040021 File or folder New Folder already exists
This seems at odds with the documentation which says if the createFolder fails it will return null.
https://docs.alfresco.com/6.1/references/API-JS-createFolder.html
I've seen several sets of code in the Hub that shows the same approach as I am using, but nothing that seems like it would mitigate this issue. Anyone have any ideas? I tried doing a try/catch block around the createFolder and that seemed to stop the exception but we always ended up in the catch block whether the createFolder should have worked or not.
In this case the documentation is indeed incorrect. The method will never return null and failures to create will be reported by exceptions, like in your case. Doing many concurrent requests / creations on the same elements is always a recipe for disaster unless retrying transaction semantics are being used - unfortunately, a FileExistsException by default will not trigger the transaction to retry unless it is manually rethrown wrapped in an exception which does, or Alfresco is reconfigured to consider FileExistsException as retryable (the latter is a bad idea, because the exception can also be triggered by bad programming, e.g. not checking if an element exists before attempting to create it).
I am not sure if one can solve this particular issue with pure JavaScript, though I have never even tried to do so. In Java code, I would use the try-catch block to rethrow the exception as a DataIntegrityViolationException. This type of exception is handled by Alfresco to automatically retry the transaction (assuming a retrying transaction is active, which is the default for any regular ReST / CMIS API calls). On the second run, the operation should then find the concurrently created folder in the existence check and skip the attempt to create it.
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.