Skip to main content
1-Visitor
March 31, 2016
Solved

Issueing transaction and rolling back in service scripts

  • March 31, 2016
  • 1 reply
  • 4045 views

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

Best answer by CarlesColl

1. Yes

2. I don't really know, tests should be done ( the same thing you did with User but with a Thing )

3. May not yes. Well, in reality when you restart Tomcat it will Rollback, which it's worst, as you are working with an unstable state until you restart...

1 reply

1-Visitor
March 31, 2016

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.

???-311-VisitorAuthor
1-Visitor
April 1, 2016

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

1-Visitor
April 1, 2016

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.