Skip to content Skip to sidebar Skip to footer

Websql Transactions With Jaydata

This question came to the JayData forum but I decided to share it here as this is an interesting topic and maybe others can benefit from it too. by V3nom » Tue Oct 23, 2012 2:06 p

Solution 1:

Transactions are not an easy thing to support on a storage agnostic manner. Therefore there is no explicit transaction management with JayData, rather you can use the implicit "all or nothing" behavior of the EntityContext. With regarding webSQL each delta package (that is: a number of add/update items and a saveChanges() at the end) run in the same webSQL transaction. If an error occures during the save of any items, all previous inserts/update will be rollbacked.

A very simple example that shows this in action: the following code inserts two items into a table, and creates a duplicate key error scenario. The end result is that there will be no rows in the table even if the second insert has been rejected being duplicate.

$data.Entity.extend("item", {
    ID: { key: true, computed: true, type: 'int' },
    data: { type: 'string' }
});

$data.EntityContext.extend("ItemsContainer", {
    items: { type: $data.EntitySet, elementType: item }
});

var offlinedb = new ItemsContainer({
    name: 'sqLite',
    databaseName: 'itemdb'
});

functioncreateLocalData() {
    offlinedb.items.add({ data: 'apple' });
    offlinedb.items.add({ data: 'orange', ID:1 });
    offlinedb.saveChanges(
        function () {

        }
    );
}

This create programmatic rollbacks you can hook the set and context level event handlers and throw an exception in them. Read more here: http://jaydata.org/blog/entitycontext-and-entityset-scoped-event-handlers-in-jaydata-1.2

Post a Comment for "Websql Transactions With Jaydata"