jeudi 5 février 2015

Nested SQlite transaction with microsoft azure mobile services managed SDK

We're using offline data sync provided by microsoft azure mobile services sdk in our WinRT project.


To ensure that data are consitent we need to make SQL transaction: So first we made a custom extension of MobileServiceSQLiteStore



public class SQLLiteStore : MobileServiceSQLiteStore
{

public SQLLiteStore(string fileName) : base(fileName) { }

public string BeginTransaction()
{
string savePointId = Guid.NewGuid().ToString();
this.ExecuteNonQuery("SAVEPOINT '" + savePointId + "';", new Dictionary<string, object>());
return savePointId;
}

public void EndTransaction(string savePointId)
{
this.ExecuteNonQuery("RELEASE SAVEPOINT '" + savePointId + "';", new Dictionary<string, object>());
}

public void RollBack(string savePointId)
{
this.ExecuteNonQuery("ROLLBACK TRANSACTION TO SAVEPOINT '" + savePointId + "';", new Dictionary<string, object>());
}
}


After That we use it like this:



SQLLiteStore store = AppServices.Instance.store; //retrieve the store object

//Get the tables services
var tableContact = AppServices.Instance.MobileService.GetSyncTable<Contact>();
var tableOrganization = AppServices.Instance.MobileService.GetSyncTable<Organization>();

//Create a simple object organization
Organization organization = new Organization()
{
DisplayName="OrganizationTest"
};

//create a contact object linked to the organization
Contact contact = new Contact()
{
Surname = "Foo",
GivenName = "Bar",
OrganizationId = organization.Id
};


string transactionSavePointID = null;

try
{
//First we begin the transaction : SAVEPOINT '<GUID>'
transactionSavePointID = store.BeginTransaction();

//try to insert both organization and contact
tableOrganization.InsertAsync(organization);
//UNCOMMENT to test transaction : throw (new Exception());
tableContact.InsertAsync(contact);

//finally we commit the transaction : RELEASE SAVEPOINT '<GUID>'
store.EndTransaction(transactionSavePointID);
}
catch (Exception)
{
//RollBack if comething was wrong
store.RollBack(transactionSavePointID);
}


Azure mobile service just changed their library. In the last version UpsertAsyncInternal function was modified and they added BEGIN and END transaction in it. Is there any way to get around that without building my own version of the libary?


The change is visible here : http://ift.tt/1KiAjkv


Regards.


Aucun commentaire:

Enregistrer un commentaire