cancel
Showing results for 
Search instead for 
Did you mean: 

Nodes not getting created

schipmolder
Champ on-the-rise
Champ on-the-rise
Hi,

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.

https://forums.alfresco.com/en/viewtopic.php?f=45&t=45367

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 {
      Thread.sleep(randomDelay);
   } catch (Exception exc) {
      exc.printStackTrace();
   }
   
   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?
2 REPLIES 2

mrogers
Star Contributor
Star Contributor
You can't catch the exception and expect to carry on since your transaction is marked for rollback.   

Equally inserting "delays" is not helping.

What you do is retry your whole transaction, if you use alfresco's retrying transaction helper (or the webscript framework) then it will do it for you. 

The first create will go through no problem.   Second create will throw an exception then succeed on the next retry.   Subsequent iterations will also find the folder and work.

schipmolder
Champ on-the-rise
Champ on-the-rise
You know, it never occurred to me that a transaction would roll back when you catch the exception, but thanks a lot that explains it all I guess.
I never really had to work with RetryingTransactionHelper before so I'll do some reading up on it.

Thanks!