Cannot enqueue Handshake after invoking quit

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

I have implemented the following code:

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


        
10条回答
  •  执笔经年
    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();
        });
      });
    };
    

提交回复
热议问题