Transaction isn't rolled back

cancel
Showing results for 
Search instead for 
Did you mean: 
ajeje93
Active Member II

Transaction isn't rolled back

Jump to solution

Hello everyone,

I have this script that copies a file in Company Home and throws and exception after the copy throws and exception.

function main(){

   try{
      var node = utils.getNodeFromString("workspace://SpacesStore/c9a77e78-2034-46ec-ae36-48049ee088d7");

      var nn = node.copy(companyhome);
      throw "exception";
   }catch(e){
      print(e.stack);
      status.setCode(status.STATUS_NOT_FOUND, e);
      return null;
   }
}
main();

I set the level of transaction to required, but the file is copied anyways.

I have three questions:

  • Why is there not a rollback?
  • What am I doing wrong?
  • How can I tell Alfresco not to copy the file if there is an exception?

Ludovico

1 Solution

Accepted Solutions
afaust
Master

Re: Transaction isn't rolled back

Jump to solution

You are throwing a JavaScript tier exception which is handled in the same script that it is thrown in. Alfresco will only deal with any exceptions that cross an API boundary or bubble up to the top-level web script call. Since you are explicitely handling the exception without rethrowing it, there is no way for the retry logic to even be aware of the exception and handle it. By handling it yourself you are specifically saying "I got this - no need to run the default rollback".

If you want Alfresco to rollback the transaction, just make sure that any exceptions are propagated properly up the call chain. If you want a specific status code and message to be set, you'd have to use the Java WebScriptException class to tell the default handling what it should set in the response.

View solution in original post

3 Replies
krutik_jayswal
Senior Member II

Re: Transaction isn't rolled back

Jump to solution

Try it using requiresnew.

Details are specified in below link.

transaction | Alfresco Documentation 

afaust
Master

Re: Transaction isn't rolled back

Jump to solution

You are throwing a JavaScript tier exception which is handled in the same script that it is thrown in. Alfresco will only deal with any exceptions that cross an API boundary or bubble up to the top-level web script call. Since you are explicitely handling the exception without rethrowing it, there is no way for the retry logic to even be aware of the exception and handle it. By handling it yourself you are specifically saying "I got this - no need to run the default rollback".

If you want Alfresco to rollback the transaction, just make sure that any exceptions are propagated properly up the call chain. If you want a specific status code and message to be set, you'd have to use the Java WebScriptException class to tell the default handling what it should set in the response.

ajeje93
Active Member II

Re: Transaction isn't rolled back

Jump to solution

Thank you very much for the explanation!