cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Issueing transaction and rolling back in service scripts

???-31
7-Bedrock

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

ACCEPTED SOLUTION

Accepted Solutions

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...

View solution in original post

6 REPLIES 6

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.

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

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.

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

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...

I understood the current "specification".


in reality when you restart Tomcat it will Rollback

I know that. But as an online systems, it is fatal that restart is required for each exception.

So I should consider rollback is N/A so far.

And, of course, I also think this is the worse problem,

you are working with an unstable state until you restart...

Anyway, I hope any improvements for these problems for the future.

Announcements


Top Tags