Forum BigDB Sure-fire way to be sure two objects are saved?

Discussion and help relating to the PlayerIO database solution, BigDB.

Sure-fire way to be sure two objects are saved?

Postby robscherer123 » May 18th, 2017, 3:38 pm

I've always kind of had this question, it's pretty basic and I never really had a complete solution to it. Is there a way to be 100% sure that two objects get saved? A simple example would be if I have a db object called MyPlayer and it has an int property called money and I need to transfer that money from MyPlayer object to MyBank. How can I be 100% sure that the money is never lost or is never duplicated?

For example, if I did something like this, then I run the risk of having the money duplicated if the MyBank.Save would happen to fail:

Code: Select all
MyPlayer.Set("money", MyPlayer.GetInt("money") + MyBank.GetInt("money"));
MyPlayer.Save(delegate(){
   MyBank.Set("money", 0);
   MyBank.Save();
});


And if I was to switch it up and save the MyBank object first and the MyPlayer second, then I run the risk of the money being lost altogether. Generally speaking, this isn't something that normally occurs but there are rare cases that always come up over the years in which a player has had their money or items "vanish" due what I believe above occurring. This seems like a common issue that many developers probably had to tackle. Is there something I'm missing, or is it just not possible to be 100% fail-safe?
robscherer123
Paid Member
 
Posts: 303
Joined: December 12th, 2012, 8:46 pm

Re: Sure-fire way to be sure two objects are saved?

Postby Henrik » May 19th, 2017, 5:39 am

https://playerio.com/documentation/refe ... aveChanges

Something like this should work:

Code: Select all
MyPlayer.Set("money", MyPlayer.GetInt("money") + MyBank.GetInt("money"));
MyBank.Set("money", 0);
PlayerIO.BigDB.SaveChanges(true, false, new DatabaseObject[]{ MyPlayer, MyBank }, delegate() {
        //Both objects saved successfully
    }, delegate(PlayerIOError error) {
        //Something went wrong, check if optimistic lock error or other error
    });
Henrik
.IO
 
Posts: 1822
Joined: January 4th, 2010, 1:53 pm

Re: Sure-fire way to be sure two objects are saved?

Postby robscherer123 » May 19th, 2017, 1:54 pm

Wow, thanks so much! This is awesome, I can't believe this was possible this whole time. This should completely eliminate me having to check my forum for cases in which this happened to users.
robscherer123
Paid Member
 
Posts: 303
Joined: December 12th, 2012, 8:46 pm

Re: Sure-fire way to be sure two objects are saved?

Postby robertflesch » May 19th, 2017, 7:50 pm

What are the advantages/disadvantages of using this method to save a bunch of object?
I can see the obvious transaction advantages, but can you see significant advantages in doing the BIG save vs 100 small saves
robertflesch
Paid Member
 
Posts: 136
Joined: April 22nd, 2013, 9:18 pm

Re: Sure-fire way to be sure two objects are saved?

Postby Henrik » May 19th, 2017, 8:38 pm

Bundling a bunch of requests together does save a little bit of bandwidth, but the semantics of the save changes to an all-or-nothing, so if there are any problems saving (one object fails optimistic locking, for example), you have to retry the entire save, which costs you extra bandwidth compared to just retrying the failed one.

Setting the fullOverwrite flag to true means you send over the entire data of all objects, which is overkill if you only needed to user it on one of the objects, etc.

So it depends on your objects and what you're expecting to happen when you save them.
Henrik
.IO
 
Posts: 1822
Joined: January 4th, 2010, 1:53 pm


Return to BigDB



cron