-1

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

});

3
  • 2
    Can you add what exactly is the error you're facing ? Apr 20, 2017 at 6:48
  • it showing e is not found Apr 20, 2017 at 7:21
  • I solved in another way. thank You for your kind attention. Apr 20, 2017 at 7:46

1 Answer 1

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

              }

         });

        } 

   });
 });
});

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.