2017-04-20 1 views
-2

expressjsを使用してsqlite3でトランザクション管理を実行する方法を教えてください。私はこの方法で試しましたが、成功しません。expressjsを使用したsqlite3のトランザクション管理

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

あなたが直面しているエラーは何ですか? –

+0

それはeを表示していません –

+0

私は別の方法で解決しました。ご静聴ありがとうございました。 –

答えて

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

       } 

     }); 

     } 

    }); 
}); 
}); 
関連する問題