6 Replies Latest reply on Jan 18, 2018 2:05 PM by ???-3 RSS
    ???-3 Explorer

    Issueing transaction and rolling back in service scripts

    I wrote a simple service below to update user information from mashups.

     

    var result;
    try{
       // (I want to do) tx = getTransaction();
        var user = Users[userName];
        user.lastName = lastName;
        user.firstName = firstName;
        // throw new Exception(); (A)
        user.emailAddress = emailAddress;
        var param = {description : description};
        user.SetDescription(param);
    
        // oldGroup -> newGroup
        if((oldGroup && newGroup) && (oldGroup != newGroup)){
            param = {member: userName};
            Groups[newGroup].AddMember(param);
            // throw new Exception(); (B)
            Groups[oldGroup].DeleteMember(param);
        }
       // (I want to do) tx.commit();
        result = "Information is successfully updated.";
        logger.info("Info of " + userName + "is updated.");
    }
    catch(err){
       // (I want to do) tx.rollback();
        result = "Failed: " + err.message;
        logger.error(result);
    }
    

     

    This script actually works, but IF error occurs, on line (A) or (B) for example, the consistency of user data can be broken.

    If I can issue transaction and do Commit or Rollback, this problem will be solved.

     

    Are there any way to do this, or alternative ideas to do something like this?

     

    Regards,

    S.Yamabe

      • Re: Issueing transaction and rolling back in service scripts
        carlesc Heavyweight Champ

        There's no Commit / Rollback ( Transactional ) feature on Server Side Javascript, it's automatically handled by platform.

         

        Try throwing a fake exception where you have // throw new Exception(), how to do it: throw "EXCEPTION_TEXT", but don't capture it, let platform capture the exception, this should cause a Rollback by the platform.

          • Re: Issueing transaction and rolling back in service scripts
            ???-3 Explorer

            Carles,

             

            Thank you for your advice.

            I've modified my script, but still not working...

             

            var result;
            try{
                var user = Users[userName];
                user.lastName = lastName;
                null.length; // throws exception
                user.firstName = firstName;
                user.emailAddress = emailAddress;
                var param = {description : description};
                user.SetDescription(param);
            
            
                if((oldGroup && newGroup) && (oldGroup != newGroup)){
                    param = {member: userName};
                    Groups[newGroup].AddMember(param);
                    Groups[oldGroup].DeleteMember(param);
                }
                result = "Information is successfully updated.";
                logger.info("Info of " + userName + "is updated.");
            }
            catch(err){
                // throw an uncaught exception for rollback
                throw "Failed: " + err.message
            }
            

             

            When running this script, I want to leave any properties unchanged.

            However, "lastName" is actually changed and saved...

             

            Looking into the log, the exception seems to be caught by Server.

            Execution error in service script [UserManageServices UpdateUserInfo] : Failed: Cannot read property "length" from null

             

            Does the script above do exactly what you mean?

             

            Regards,

            S.Yamabe

              • Re: Issueing transaction and rolling back in service scripts
                carlesc Heavyweight Champ

                Hi S.Yamabe,

                 

                Well the DataBase get's on an unstable state, as if you restart tomcat you will get previous value , hence the Rollback really happens.. -- Maybe we should open a case for this, to investigate if it --> Scaring.

                 

                But by the way, it may not work exactly with users that with Things... A Thing can be Restarted a User not.

                 

                Carles.

                  • Re: Issueing transaction and rolling back in service scripts
                    ???-3 Explorer

                    Carles,

                     

                    Thank you. Let me confirm what you mean. (because I'm not very good at understanding English.. )

                     

                    1. For my script, it is required for Tomcat to be restarted so that I get the previous value of lastName.

                    2. If the object entities are "Thing"s, restart is(should be) not required.

                    3. User entities may not be rollback-able.

                     

                    Are these true?

                     

                    S.Yamabe