使用expressjs在sqlite3中进行事务管理 [英] transaction management in sqlite3 using expressjs

查看:98
本文介绍了使用expressjs在sqlite3中进行事务管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请指导我如何使用expressjs在sqlite3中执行事务管理.我以这种方式尝试过,但没有成功.

Please guide me how to perform transaction management in sqlite3 using expressjs. I tried in this way but am not succeed.

app.get('/transaction', function(req, res){
db.serialize(function() {

    try{
        db.run("BEGIN");
        db.run("UPDATE emp SET balance = 10000 WHERE eid = 1", function(err, row){
            if (err){
                throw (e);
            }
        });
        db.run("UPDATE temp SET deptno = 2000 WHERE eid = 2", function(err, row){
            //this temp table not exists it should be rollback and server should not 
            //stop
            if (err){
                throw (e);
            }
        });

        db.run('commit');
        res.end("Transaction succeed");

    }//try
    catch(e){
        //console.log(e);
        res.end("Transaction cancelled");
        db.run('rollback');
        //console.log(e);
    }//catch
});

});

推荐答案

app.get('/transaction', function(req, res){
db.serialize(function() {  

db.run("BEGIN");

db.run("UPDATE emp SET deptno = 10 WHERE eid = 1", function(err, row){

    if (err){

       console.log(err); 
       res.end("Transaction cancelled"); 

    }

    else{

     db.run("UPDATE temp SET deptno = 20 WHERE eid = 2", function(err, row){

           if (err){

              console.log(err); 

              db.rollback;

              res.end("Transaction cancelled");

             }

          else{

              db.run('commit');

              res.end("Transaction succeed");

              }

         });

        } 

   });
 });
});

这篇关于使用expressjs在sqlite3中进行事务管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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