webSQL transactions with JayData

Deadly 提交于 2019-12-13 03:44:26

问题


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


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

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



来源:https://stackoverflow.com/questions/13036693/websql-transactions-with-jaydata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!