Re: [persistence.js] Caching a transaction

106 views
Skip to first unread message

Zef Hemel

unread,
May 3, 2013, 7:19:52 AM5/3/13
to persis...@googlegroups.com
No, I think the transaction has to be used straight away, it cannot be cached. This is not a persistence.js issue, but a WebSQL one.

-- Zef


On Fri, May 3, 2013 at 1:01 PM, Manners O <manners...@gmail.com> wrote:
Hi all, 

I'm trying to use transactions in this scenario.
1 - I create a transaction and cache its value:
    var cachedTransaction;
    persistence.transaction(function (err, tx) {
        if (!err) {
            cachedTransaction = tx;
        }
    });

2 - I use this cached transaction in subsequent DB operations like so:
    var logAllUsers = function () {
        User.all().list(cachedTransaction, function(err, users) {
              users.forEach(function(user) {
                   console.log(user.name);
              });
         });
    };
    var logOneUser = function () {
         User.all().one(cachedTransaction, function(err, user) {
              console.log(user.name);
         });
    };

....etc

but this gives me the following error:
  1. Uncaught Error: InvalidStateError: DOM Exception 11 persistence.store.websql.js:81

Is is possible to cache a transaction in this way and use it in multiple queries??

Thanks
Manners.

--
You received this message because you are subscribed to the Google Groups "persistence.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to persistencej...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark M. Young

unread,
May 3, 2013, 12:26:58 PM5/3/13
to persis...@googlegroups.com
Manners, just to be clear, you can however perform multiple statements within a persistence.transaction.  Does that makes sense?
--
"When pride comes, then comes disgrace, but with humility comes wisdom."
—Proverbs 11:2

Zef Hemel

unread,
May 14, 2013, 11:35:59 AM5/14/13
to persis...@googlegroups.com
I think the WebSQL implementation is such that all queries part of a transaction need to be executed in one go, without any other (non-WebSQL) asynchronous operations (such as time outs, or probably AJAX calls) in between. I have no idea why, but in my experience otherwise you indeed start to get these types of errors. As a result I never really used transactions as "real" transactions.

-- Zef


On Tue, May 14, 2013 at 4:42 PM, Manners O <manners...@gmail.com> wrote:
Hi all, 

I'm stil carrying out some test with transactions. I've moved away from caching the transaction and I'm now passing the transaction object to various functions that will later be executed as part of one "operation".

I've created a jsfiddle to demonstrate this: http://jsfiddle.net/gotomanners/y7UBV/4/

So continuing on from my example above, I pass the tx as an argument to `logAllUsers` and `logOneUser` and it runs fine as Mark said, multiple queries, one transaction. 
See Operation 1 in jsFiddle:
    persistence.transaction(function (err, tx) {
        if (!err) {
            logOneUser(tx); // one
            logAllUsers(tx); // all
         }
    });

but when I do the same as above but add a timeout between queries `logOneUser` and `logAllUsers` (simulating an ajax call or something) I get the same error as above.(See console)
See Operation 2 in jsFiddle:
    persistence.transaction(function (err, tx) {
        if (!err) {
            logOneUser(tx); // one
            window.setTimeout(function() { // wait 400ms or any ms
                logAllUsers(tx); // all
            }, 400);     
        }
    });
  1. Uncaught Error: InvalidStateError: DOM Exception 11 persistence.store.websql.js:81

Does anyone understand what is going on here?
I thought maybe the tx object was lost due to different scope but that is not the case.

Manners O

unread,
May 14, 2013, 11:44:20 AM5/14/13
to persis...@googlegroups.com, z...@zef.me
Hi Zef, 

Thanks, for the explanation. Its quite odd that it works that way.
I'm refactoring some old code to use transactions in order to boost performance and I've already seen improvements.
In what way do you use transactions if you don't mind me asking?

Manners O

unread,
May 14, 2013, 12:42:43 PM5/14/13
to persis...@googlegroups.com, z...@zef.me
Hi all, 

Operation 1, from the example above seems to work for various levels of nested queries (with callbacks) but only fails as soon as any other (non-WebSQL) operation like Zef said, is carried out.

I've just ran a large operation taking over 7 minutes to complete on a single transaction and it completed successfully. 
I guess this rules out my thought that the `setTimeout` or ajax call causes the transaction to close because of a timeout.

Manners

Ken Corbett

unread,
May 14, 2013, 8:00:41 PM5/14/13
to persis...@googlegroups.com, z...@zef.me
It is an interesting question.  I always had thought the transaction was committed just before the transaction object was garbage collected, but apparently this is not true.  I wonder what the true conditions are.

Ken Corbett

Manners O

unread,
May 15, 2013, 4:42:23 AM5/15/13
to persis...@googlegroups.com, z...@zef.me
Hi Ken,

I thought the same too but with my tests it seems as though the transaction is forcibly closed in its own context if you carry out and other time consuming non-Websql operation. 
According to the spec on executing sql statements, (http://www.w3.org/TR/webdatabase/#executing-sql-statements-0), that error is thrown when the transaction is stale. It doesn't say what causes it to go stale. 
If anyone can shed more light, please do.

Thanks,
Manners
Reply all
Reply to author
Forward
0 new messages