Question

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 pm

I struggle to find information on websql transactions in JayData. Can anyone give a hint or a link ?

http://jaydata.org/forum/viewtopic.php?f=3&t=101&sid=8accd7bf5bed872b6e88d36004a280c5

Was it helpful?

Solution

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'
});

function createLocalData() {
    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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top