Create Folder causing exception when when created concurrently

Showing results for 
Search instead for 
Did you mean: 

Create Folder causing exception when when created concurrently


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.

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.

1 Reply

Re: Create Folder causing exception when when created concurrently

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.