Cannot enqueue Handshake after invoking quit

前端 未结 10 2195
小鲜肉
小鲜肉 2020-11-28 02:59

I have implemented the following code:

module.exports = {
    getDataFromUserGps: function(callback)
    {
        connection.connect();
        connection.qu         


        
相关标签:
10条回答
  • 2020-11-28 03:53

    If you using the node-mysql module, just remove the .connect and .end. Just solved the problem myself. Apparently they pushed in unnecessary code in their last iteration that is also bugged. You don't need to connect if you have already ran the createConnection call

    0 讨论(0)
  • 2020-11-28 03:58

    I had the same problem and Google led me here. I agree with @Ata that it's not right to just remove end(). After further Googling, I think using pooling is a better way.

    node-mysql doc about pooling

    It's like this:

    var mysql = require('mysql');
    var pool  = mysql.createPool(...);
    
    pool.getConnection(function(err, connection) {
        connection.query( 'bla bla', function(err, rows) {
            connection.release();
        });
    });
    
    0 讨论(0)
  • 2020-11-28 04:01

    AWS Lambda functions

    Use mysql.createPool() with connection.destroy()

    This way, new invocations use the established pool, but don't keep the function running. Even though you don't get the full benefit of pooling (each new connection uses a new connection instead of an existing one), it makes it so that a second invocation can establish a new connection without the previous one having to be closed first.

    Regarding connection.end()

    This can cause a subsequent invocation to throw an error. The invocation will still retry later and work, but with a delay.

    Regarding mysql.createPool() with connection.release()

    The Lambda function will keep running until the scheduled timeout, as there is still an open connection.

    Code example

    const mysql = require('mysql');
    
    const pool = mysql.createPool({
      connectionLimit: 100,
      host:     process.env.DATABASE_HOST,
      user:     process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,
    });
    
    exports.handler = (event) => {
      pool.getConnection((error, connection) => {
        if (error) throw error;
        connection.query(`
          INSERT INTO table_name (event) VALUES ('${event}')
        `, function(error, results, fields) {
          if (error) throw error;
          connection.destroy();
        });
      });
    };
    
    0 讨论(0)
  • 2020-11-28 04:01

    If you're trying to get a lambda, I found that ending the handler with context.done() got the lambda to finish. Before adding that 1 line, It would just run and run until it timed out.

    0 讨论(0)
提交回复
热议问题