AnsweredAssumed Answered

Nodes not getting created

Question asked by schipmolder on Dec 12, 2012
Latest reply on Dec 12, 2012 by schipmolder

I've been running into an issue for a while now, but it's rather difficult to explain.
I guess it's similar to the issue described in the post below but as mine isn't CMIS related I wanted to open a new thread.

My problem is that I've got a webscript that creates a folder if it doesn't exist yet and then places some content in that folder.
This works fine, but the problem starts when two, practically simultaneous, requests are sent to that webscript.

The first request will detect that the folder doesn't exist yet and creates it without a problem, but the second request also detects that the folder doesn't exist yet (fileFolderService.searchSimple()), tries to create it (fileFolderService.create()) which fails as the first request has just created it.
I suspected that this was caused by the index not having updated yet so I built in a random delay to make sure there's at least a few seconds between the two requests detecting if the folder exists, but oddly enough this doesn't seem to make a difference.

I then tried what would happen if I try to find the folder in the exception handler of the fileFolderService.create() call and then it suddenly does find the correct folder noderef…

At that stage I thought I had an acceptable workaround but although I now have the correct NodeRef for the folder, no matter what I do with it Alfresco just doesn't actually save it.
For example, if I create a file in that folder using fileFolderService.create() it doesn't throw any exceptions and returns a new NodeRef fine, but when I then search for that NodeRef in the Node Browser in Share it doesn't actually exist, nor can i find any trace of it!

So basically I have this code:

NodeRef parentFolderRef => the NodeRef to an existing folder inside which a subfolder should be created
String subfolderName => the name of the subfolder, i.e. "myFolder"

NodeRef subfolderRef = fileFolderService.searchSimple(parentFolderRef, subfolderName);

if (subfolderRef == null) {
   // Add a random delay to prevent both webscript requests from creating the same folder at the same time
   Random randomGenerator = new Random();
   int randomDelay = randomGenerator.nextInt(60000);
   try {
   } catch (Exception exc) {
   try {
      FileInfo subfolderInfo = fileFolderService.create(parentFolderRef, subfolderName, ContentModel.TYPE_FOLDER);
      // An exception is thrown here as the folder has already been created by the first webscript request
   } catch (FileExistsException e) {
      // try to find the sub folder again as the filefolder service claims it does exist!
      subfolderRef = fileFolderService.searchSimple(parentFolderRef, subfolderName);
      // Now I do actually get the correct NodeRef as subfolderRef!
      FileInfo secondSubfolderInfo = fileFolderService.create(subfolderRef, "anotherSubFolder", ContentModel.TYPE_FOLDER);
      NodeRef secondSubfolderRef = secondSubfolderInfo.getNodeRef();
      // secondSubfolderInfo is a normal NodeRef and all looks to work ok, but when i check later in the node browser the 'anotherSubFolder' doesn't actually exist!


This is a rather big issue for me obviously as there's no way to detect errors yet the second sub folder never actually gets created!

I'm running Alfresco community 4.0.d and am planning to update to 4.2 but can't right now for unrelated reasons.

Does anybody have any ideas?