How are pool.query () and pool.getGetConnection () different in connection.release ()?

As I understand it, everyone pool.query()will be worth the connection, and it will automatically be released when it ends. based on this comment github issue . But what about nested queries executed with pool.getConnection()?

pool.getConnection(function(err, connection) {

  // First query
  connection.query('query_1', function (error, results, fields) {

    // Second query
    connection.query('query_2', function (error, results, fields) {

          // Release the connection
          // DOES THIS ALSO RELEASE query_1?
          connection.release();

          if (error) throw error;

          // you can't use connection any longer here..
    });
  });
});

UPDATE

Here is my code using a transaction when executing nested queries.

const pool = require('../config/db');

function create(request, response) {
   try {

       pool.getConnection(function(err, con) {

           if (err) {
               con.release();
               throw err;
           }

           con.beginTransaction(function(t_err) {

               if (t_err) {
                   con.rollback(function() {
                      con.release();
                      throw t_err;
                   });
               }


               con.query(`insert record`, [data], function(i_err, result, fields){

                   if (i_err) {
                       con.rollback(function() {
                           con.release();
                           throw i_err;
                       });
                   }


                   // get inserted record id.
                   const id = result.insertId;

                   con.query(`update query`, [data, id], function(u_err, result, fields)=> {

                       if (u_err) {
                          con.rollback(function() {
                             con.release();
                             throw u_err;
                          });
                       }

                       con.commit(function(c_err){
                          if (c_err) {
                             con.release();
                             throw c_err;
                          }
                       });

                       con.release();

                       if (err) throw err;

                       response.send({ msg: 'Successful' });
                   });
               });

           });
       });

   } catch (err) {
      throw err;
   }
}

I made a lot of protective mistakes and con.release(), since at the moment I do not know how to properly release each active connection.

And I also assume that everyone con.query()inside pool.getConnection()will be worth the mix.

+4
source share
3 answers

EDIT:

A connection , . , connection.query() , , , .

pool connection, "", , . release() , , , .

, a query - . , , .


pool.query(statement, callback)

const query = (statement, callback) => {
    pool.getConnection((err, conn) => {
        if(err) {
            callback(err);    
        } else {
            conn.query(statement, (error, results, fields) => {
                conn.release();
                callback(error, results, fields);
            });
        }
    })
}

, . multipleStatements: true , .

BEGIN;
INSERT ...;
SELECT LAST_INSERT_ID() INTO @lastId;
UPDATE ...;
COMMIT;
+1

, , .

, . , insertid.

( :) "". ( ):

BEGIN;
INSERT ...;
get insertid
UPDATE ...;
COMMIT;

- , . "". INSERT UPDATE, "" SQL-. get insertid - -, SQL.

+1

Node.js, , Github, , pool.getConnection , . , , , , , mysql ( ). , , ; pool.getConnection "" , "in_use", .

As a rule, after receiving a connection from the connection pool, it can be used for any number of operations / requests, and it is issued “once” to return it to the “free” list of the pool. This is how joining usually works.

0
source

Source: https://habr.com/ru/post/1688193/


All Articles