WebSQL transactions with JayData

This question came to the JayData forum, but I decided to share it here because it is an interesting topic, and maybe others can benefit from it.

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

I'm struggling to find websql transaction information in JayData. Can someone give a hint or link?

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

0
source share
1 answer

Transactions are not easy to maintain agnostically. Therefore, there is no explicit transaction management with JayData, rather, you can use the implicit behavior of the "all or nothing" EntityContext. As for webSQL, each delta package (i.e.: the number of add/update and saveChanges() elements at the end) is executed in the same webSQL transaction. If an error occurs while saving any items, all previous inserts / updates will be discarded.

A very simple example that shows this in action: the following code inserts two elements into a table and creates a script for duplicating key errors. The end result is that there will be no rows in the table, even if the second insert was rejected as a 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 creates software rollbacks that you can hook up dial-up and context-level event handlers and throw an exception in them. More details here: http://jaydata.org/blog/entitycontext-and-entityset-scoped-event-handlers-in-jaydata-1.2

+3
source

Source: https://habr.com/ru/post/1445852/


All Articles