Forum BigDB Loading a database object while saving

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

Loading a database object while saving

Postby supermoose » April 6th, 2015, 7:51 pm

Hello! I'm getting some inconsistent behavior in my application while I am making heavy use of the saving/loading. Since it seems to be occurring randomly, I assume that I've created a race condition somewhere. I am wondering if it is possible to load a databseObject mid-save, and have some properties missing. My code works like this right now:

Code: Select all
public void processCommands()
{
    PlayerIO.BigDB.Load("MyObject", ConnectUserId, delegate (DatabaseObject result){
        if (result != null) {
            //Change a property and save back.
            DatabaseObject newObj = new DatabaseObject()
            newObj.Set("Property1", "Value1");
            newObj.Set("Property2", "Value2");
            result.Set("ComplexObject", newObj);
            result.Save(null);
            PlayerIO.BigDB.Load("MyObject", ConnectUserId, delegate (DatabaseObject result2){
                if (result2 != null) {
                    //Check a property and save back.
                    DatabseObject oldObj = result2.GetDatabaseObject("ComplexObject");
                    if(oldObj !=null)
                    {
                        string value1 = oldObj.GetString("Property1");
                        string value2 = oldObj.GetString("Property2");
                    }
                    //Do Some Work Here Not Regarding The ComplexObject
                    result2.Save(null);
                }
            });
        }
    });
}


My Question is then: Is it possible for my second load call (Which happens as soon as the previous save is kicked off - but it is not a callback) to get the database object mid-save? The behavior I am seeing is that when I add a child databaseObject, some of its properties will go missing. In the code above, the ComplexObject may exist, and Property1 may have its value, but property2 would be completely missing. And I am worrying then, that calling save again (without using an optimistic lock) would lock that property2 as missing permanently.

Is that possible? Or does the Save() method block load() commands during its write process?
supermoose
 
Posts: 2
Joined: January 29th, 2012, 1:52 am

Re: Loading a database object while saving

Postby Henrik » April 7th, 2015, 10:37 pm

Save and Load are both asynchronous operations, which means that the callback in the loading of result2 might fire before the save of result has happened. So the result2 object may or may not have the property ComplexObject, and when you save it back you will overwrite the save of the result object.

It's also a bit weird to load the object when you already have it, unless you're certain you've concurrently modified it somewhere else, in which case you really should be using optimistic locking, otherwise you'll be overwriting your objects willy-nilly.

https://gamesnet.yahoo.net/documentatio ... b/advanced
Henrik
.IO
 
Posts: 1822
Joined: January 4th, 2010, 1:53 pm

Re: Loading a database object while saving

Postby supermoose » April 7th, 2015, 11:49 pm

Hey Henrik!

Thanks for your help on this. I understand that The two operations can happen in either order. But what I am more worried about is if the load can happen DURING the save, to get corrupt data back from BigDb, not just out-of-date data.

The behavior I've been seeing (with rather large objects being saved) is that after I would call Save(), the new ComplexObject has been created in the database, but does not have all of its properties. My code is set up to handle whether the ComplexObject exists or doesn't exist, but it is not set up to handle it partially existing.

So, in the example above, the second Load() call would get back the DabaseObject, and it would have ComplexObject inside of it, but ComplexObject would only have finished saving Property1, and would have no Property2 at all, even though they were both set during the same Save() call. Is this possible?

And yes, this would be solved by optimistic locking, but I was unaware of that option when I first started on my project, so it would be a pain to refactor at this point.
supermoose
 
Posts: 2
Joined: January 29th, 2012, 1:52 am

Re: Loading a database object while saving

Postby Henrik » April 15th, 2015, 4:18 am

No, saves to BigDB objects are atomic, you can not get partial saves. But without optimistic locking you can have saves coming in from two places that overwrite each other.
Henrik
.IO
 
Posts: 1822
Joined: January 4th, 2010, 1:53 pm


Return to BigDB



cron