简体   繁体   中英

node-sqlserver, Azure Mobile Services and Azure SQL - old rows are returned even after transaction commit

I have following code on server side:

let query = `
       BEGIN TRANSACTION FOO_TRAN
       EXEC sp1_update ...,
       EXEC sp2_insert ...,
       EXEC sp3_update ...,
       EXEC sp4_delete ...,
       ...
       COMMIT TRANSACTION FOO_TRAN
       SELECT 1 as [@@@];
    `;
mssql.query(query, params, {
    success:  function (res) {
        if (res && res.length === 1 && res[0]['@@@'] == 1) {
            response.status(200).send({id: request.body.id});
        }
    }, error: (err)=>response.status(500).send(err)
});

Then client immediately requests modified content using provided id .

Problem: old data is returned for ~2-3 seconds. I tried to specify READ UNCOMMITED in subsequent SELECT, but it didn't help - old rows were mixed with new ones.

To use transactions with Azure Mobile Services you'll want to use the open method on mssql to get a connection which supports transactions. See documentation of open method here . For example:

request.service.mssql.open({

    success: function(connection) {

        //start a transaction
        connection.beginTransaction(function(errTransaction) {

            if (errTransaction) {
                //handle the error and respond error
                connection.close();
                return;
            }

            //define a queryString and queryParams
            connection.query(queryString, queryParams, function(errQuery, results) {

                if (errQuery) {
                    //handle the error and respond error
                    connection.rollback();
                    connection.close();
                    return;
                }

                //success
                connection.commit();
                connection.close();
                //respond OK
            });                                         

        });
    },

    error: function(errOpen) {
        //handle the error
    }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM