transaction management in sqlite3 using expressjs

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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 }); 

});

回答1:

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");                }           });          }      });  }); }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!