Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
AWS Lambda Node.js. mysql Error "Cannot enqueue Query after invoking quit" on second call onwards:

I am currently creating an AWS Lambda resolver that utilizes Nodejs mysql library. Right now, my lambda is capable of executing a query to the database during the first invocation. However, on the subsequent invocations the error "Cannot enqueue Query after invoking quit" occurs. I am aware that it is something to do with the connection to the database but if I am not wrong, 'connection.query' should be making an implicit connection to the database. I am also calling the 'connection.end' within the callback. Could someone lend me a helping hand on this one?

Here is the Lambda index.js code:

/* Amplify Params - DO NOT EDIT
    API_SIMPLETWITTERCLONE_GRAPHQLAPIENDPOINTOUTPUT
    API_SIMPLETWITTERCLONE_GRAPHQLAPIIDOUTPUT
    API_SIMPLETWITTERCLONE_GRAPHQLAPIKEYOUTPUT
    AUTH_SIMPLETWITTERCLONEB1022521_USERPOOLID
    ENV
    REGION
Amplify Params - DO NOT EDIT */

const mysql = require("mysql");
// const util = require("util");
const config = require("./config.json");

const connection = mysql.createConnection({
  host: config.dbhost,
  user: config.dbuser,
  password: config.dbpassword,
  database: config.dbname,
});

// resolvers
const resolvers = {
  Query: {
    getAllUser: (event) => {
      return getAllUser();
    },
  },
};

function executeQuery(sql, params) {
  return new Promise((resolve, reject) => {
    const queryCallback = (error, results) => {
      if (error) {
        console.log("Error occured during query: " + error.message);
        connection.destroy();
        reject(error);
      } else {
        console.log("Connected to database and executed query");
        console.log("Results:", results);
        connection.end((err) => {
          if (err) {
            console.log("there was an error closing database:" + err.message);
          }
          console.log("database closed");
          resolve(results);
        });
      }
    };
    if (params) {
      connection.query(sql, [...params], (error, results) => {
        queryCallback(error, results);
      });
    } else {
      connection.query(sql, (error, results) => {
        queryCallback(error, results);
      });
    }
  });
}

async function getAllUser() {
  const sql = "SELECT * FROM User";
  try {
    const users = await executeQuery(sql);
    return users;
  } catch (error) {
    throw error;
  }
}

exports.handler = async (event) => {
  // TODO implement
  console.log("event", event);
  console.log('DB connection var', connection);
  const typeHandler = resolvers[event.typeName];
  if (typeHandler) {
    const resolver = typeHandler[event.fieldName];
    if (resolver) {
      try {
        return await resolver(event);
      } catch (error) {
        throw error;
      }
    }
  }
  throw new Error("Resolver not found");
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are ending your connection during the first invocation. As a result, the second invocation does not have that connection anymore.

If you create the connection object during module import, do not .end() it in your invocation.

If you wish to .end() the connection object during invocation, you have to create it during invocation as well.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!