cancel
Showing results for 
Search instead for 
Did you mean: 

How to restart the repository session

ethan
Champ in-the-making
Champ in-the-making
Hi Smiley Happy

I'm trying to add some nodes to the Alfresco Repository. I iterate through a tree and copy each nodes and their children into the Alfresco repository. Sometimes, an exception is thrown (like DuplicateChildNameException). I used a try catch for each node and I would like that when an exception is thrown, the current node would be ignored and the iterator continue to do its work.

The problem is when an Exception is thrown, the session doesn't work anymore. There is an error message for each following nodes. Something like "the transaction has already been rolled back".

I tried to logout from the session but it threw an illegalStateException. I tried to reconnect to the repository with the same session variable (sessionALF = repository.login(myCredentials):smileywink: but it threw an exception with the following message: "only one session is allowed per thread".

Do you have an idea about what I should do?

Thank you Smiley Happy
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
Unfortunately you can't catch the exception and carry on since the current transaction is rolling back.

If it is a big long list of nodes then do you need to process them all as one transaction or could you process them in individual transactions?

If you do need a big long list then you need to do as much validation up front as possible.

ethan
Champ in-the-making
Champ in-the-making
Currently, I save each node at a time. I use the following code :

try {
   String nodeRelPath = ISO9075.encode(nodeName);
         
   if(!alfParentNode.hasNode(nodeRelPath)){
      try{
         Node newAlfNode = alfParentNode.addNode(nodeRelPath, customNodeType);
         newAlfNode.setProperty("cm:name", "myNodeName");
         …
         sessionALF.save();
         convertChildrenNodes(sessionALF, childNode, newAlfNode);
               
      } catch(RepositoryException e){
         System.out.println("ERROR WHILE SAVING NODE :" + nodeName + " - " + e.getCause());
      }
                     
   } else {
      Node newAlfParentNode = alfParentNode.getNode(nodeRelPath);
      System.out.println("already existing node : " + nodeName);
      convertChildrenNodes(sessionALF, childNode, newAlfParentNode);
   }

} catch (RepositoryException e) {
   e.printStackTrace();
}

ethan
Champ in-the-making
Champ in-the-making
Up !

Is this the right way to proceed or is there a better one ?

Thank you Smiley Happy

ethan
Champ in-the-making
Champ in-the-making
Should I use explicit transactions like it is defined here in the wiki ? If I use the trx.rollback() in the catch() part, will I still be able to use the session and get back to the parent node to continue to iterate?