Mister Smith
Mister Smith

Reputation: 28178

Is it possible to nest sentences in the same transaction?

I'm currently playing around with the asynchronous API of WebSql. Given this code:

        db.transaction(
            function (tx) {                      
                tx.executeSql("SELECT * FROM table",
                    [],
                    function(t, resultSet){ //Anonimous function implementing SQLStatementCallback
                        t.executeSql(...); //#1 
                    }
                ); 
            },
            function (err) {
                console.error("Error in transaction");                         
            },
            function(){
                console.log("Transaction complete"); //#2                  
            }   
        );

I could not find this in the spec. The third parameter in executeSql is a function inplementing SQLStatementCallback . The first parameter in this interface is another SQLTransaction (named t in my code). Would it be possible to use this transaction object to continue executing sentences? In particular:

  1. Is t the same as tx?
  2. Could I use t to execute another SQL sentence, and in this case is #1 guaranteed to run before #2?

Upvotes: 0

Views: 180

Answers (2)

Kyaw Tun
Kyaw Tun

Reputation: 13141

  1. Yes, transaction object is same in t and tx.

  2. Yes, It is grantee to run 1# before 2# since you have already listen onsuccess handler callback. I want to achieve ordering of the requests, i use t. Whenever I want to let then run in parallel I use tx. See the code in my websql request executor implementation YDN-DB library.

Upvotes: 1

CL.
CL.

Reputation: 180162

Yes, this is how the WebSQL API works.

Because of the asynchronous execution, this is the only way to execute multiple commands in one transaction.

Upvotes: 0

Related Questions