JayData的WebSQL事务 [英] webSQL transactions with JayData

查看:91
本文介绍了JayData的WebSQL事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来到了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»2012年10月23日,星期二,下午2:06

by V3nom » Tue Oct 23, 2012 2:06 pm

我很难在JayData中找到有关Websql事务的信息.任何人都可以给出提示或链接吗?

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

推荐答案

以与存储无关的方式来支持事务并不是一件容易的事.因此,没有JayData的显式事务管理,而是可以使用EntityContext的隐式全有或全无"行为.关于webSQL,每个增量包(即:多个add/update项目和最后一个saveChanges())都在同一webSQL事务中运行.如果在保存任何项目期间发生错误,则将回滚所有先前的插入/更新.

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

        }
    );
}

这将创建编程的回滚,您可以挂钩集合和上下文级别的事件处理程序,并在其中引发异常.在此处了解更多信息: http://jaydata.org/blog/entitycontext-and-entityset-scoped-event-handlers-jaydata-1.2

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

这篇关于JayData的WebSQL事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆