connection.release();在node.js中的connection.beginTransaction中没有释放连接

时间:2018-07-21 05:45:57

标签: mysql node.js

  

我正在使用 node.js mysql 。我想通过事务在表中插入/更新数据,因为只有在某些情况下,我才想插入/更新数据。为此,我将池和连接与事务一起使用。下面的代码在表中的插入/更新数据上工作正常。问题是插入/更新后我使用connection.release()释放连接。但是连接未正确释放

var jsonResponse = JSON.stringify
           ({
            Data: ''
        });
var AgholdId = 0;
var DocId = req.body.params.DocId;
pool.getConnection(function (err, connection) { 
    connection.beginTransaction(function (err) {                           
         if (err) { throw err; }                                        
            async.series([  function                             
                  function (callback) {                                 
                     try {                                               
                           if (req.body.params.AgholdAction == "Add" && req.body.params.AgholdId == 0) {                                          
                              query = "CALL spAgholdInsert(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";           
                              var param = [ req.body.params.StatusId , req.body.params.PRNId , req.body.params.MAWBId, req.body.params.Pieces , req.body.params.FullBoxEqv,
                                req.body.params.Quantity , req.body.params.ItemName, req.body.params.ItemId, req.body.params.AgholdDocId, 
                                req.body.params.MAWBDocId, req.body.params.HAWBNumber, req.body.params.GTSInvoiceId ,
                                DocId, req.body.params.InvoiceNumber, req.body.params.EightBox, req.body.params.HalfBox, req.body.params.QuarterBox, req.body.params.FullBox, req.body.params.UOM, req.session.ClientId, formatteddatetime , req.session.UserId,
                                formatteddatetime , req.session.UserId                             
                              ];                    
                              connection.query(query, param, function (err, result) {                                                              
                                  if (err) {         
                                        connection.rollback();
                                        jsonResponse = JSON.stringify({
                                        Data: err.message,
                                        Status: 'Invalid'
                                        });
                                        connection.release();
                                        res.end(jsonResponse);                             
                                  }                                    
                                  else {                           
                                        AgholdId = result[0][0].AgholdId;
                                        Action = "Add";
                                        callback();                                 
                                  }                                                            
                              });                                      

                           }                                           
                           else if (req.body.params.AgholdAction == "Edit" && req.body.params.AgholdId > 0) {                                        
                               query = "CALL spAgholdUpdate(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";               
                               var param = [ req.body.params.PRNId , req.body.params.MAWBId, req.body.params.Pieces , req.body.params.FullBoxEqv,
                                req.body.params.Quantity , req.body.params.ItemName , req.body.params.ItemId,
                                req.body.params.MAWBDocId, req.body.params.HAWBNumber, req.body.params.GTSInvoiceId ,
                                DocId, req.body.params.InvoiceNumber, req.body.params.EightBox, req.body.params.HalfBox, req.body.params.QuarterBox, req.body.params.FullBox, req.body.params.UOM, req.session.ClientId ,
                                formatteddatetime, req.session.UserId, req.body.params.AgholdId, req.body.params.IsUpload                            
                               ];                   
                               connection.query(query, param, function (err, result) {                                                              
                                  if (err) {         
                                      connection.rollback();
                                      jsonResponse = JSON.stringify({
                                      Data: err.message,
                                      Status: 'Invalid'
                                      });
                                      connection.release();
                                      res.end(jsonResponse);                            
                                  }                                    
                                  else {                           
                                      AgholdId = 0;
                                      Action = "Update";
                                      callback();                                      
                                  }

                               });                                         
                             }                                    
                     }                                                
                     catch (excb4) {                                    
                            var jsonResponse = JSON.stringify({
                            Data: excb4.message,
                            Status: 'Invalid'
                            });
                            connection.release();
                            res.end(jsonResponse);                                    
                     }                                 
                  },                                               
                  function (callback) {          
                          connection.commit(function (err) {             
                             if (err) {              
                                 connection.rollback();
                                 jsonResponse = JSON.stringify({
                                 Data: err.message,
                                 Status: 'Invalid'
                                 });                                 
                             }                                       
                             else{                 
                                  console.log('Transaction Complete.');
                                  jsonResponse = JSON.stringify({
                                  Data: 'Record inserted successfully',
                                  Status: 'Valid',
                                  AgholdId: AgholdId,
                                  Action: Action
                                });                                        
                             }                            
                             connection.release();
                             res.end(jsonResponse);
                          });                                   
                  }                                         
            ]);  

        });                                                                        
    }); 

0 个答案:

没有答案