Lajtovo
Lajtovo

Reputation: 115

Reusing Mysql Connection in Lambda on AWS

I have a lot of lambda invocations and I am always querying database so I want my lambda to be able to reusing connection, so I am establishing connection in constructor, like this:

let mysqlConnection = mysql.createConnection({
  host: process.env.HOST,
  user: process.env.DB_USER,
  password: process.env.BDB_PWD,
  port: process.env.DB_PORT,
  database: process.env.DB_NAME
});

dbConn.connect(function(err) {
  if (err) {
    throw new Error('Error during connecting to db');
  } else {
    console.log("Database is connected");
  }
});

And I have callbackWaitsForEmptyEventLoop set to false in my handler:

exports.handler = (event, context) => {
  context.callbackWaitsForEmptyEventLoop = false;
  event.Records.forEach(record => {
   processEvent(JSON.parse(record.body), context);
  });
 };

When mysql connection fails I am destroying mysql connection, my query in provessEvent function looks like this:

  mysqlConnection.query(sql, values, function (err, rows) {
    if (err) {
      mysqlConnection.destroy();
      context.fail();
    } 
  }

But I am getting a lambda errrors from time to time. I guess sometimes lambda wants to reuse a connection which is not available.

 "Error: Error during connecting to db",
    "    at Handshake.<anonymous> (/var/task/lambda.js:34:13)",
    "    at Handshake.<anonymous> (/var/task/node_modules/mysql/lib/Connection.js:525:10)",
    "    at Handshake._callback (/var/task/node_modules/mysql/lib/Connection.js:491:16)",
    "    at Handshake.Sequence.end (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)",
    "    at Protocol.handleNetworkError (/var/task/node_modules/mysql/lib/protocol/Protocol.js:369:14)",
    "    at Connection._handleNetworkError (/var/task/node_modules/mysql/lib/Connection.js:421:18)",
    "    at Connection._handleConnectTimeout (/var/task/node_modules/mysql/lib/Connection.js:417:8)",
    "    at Object.onceWrapper (events.js:286:20)",
    "    at Socket.emit (events.js:198:13)",
    "    at Socket.EventEmitter.emit (domain.js:448:20)"

Is there anything could I do to prevent this situation? Or should I destroy connection and connect once again?

Upvotes: 2

Views: 2998

Answers (2)

Tuan Vo
Tuan Vo

Reputation: 2065

I do not use MySQL but Mongodb in some projects. callbackWaitsForEmptyEventLoop was help.

'use strict';

const mysql = require('mysql'); // require mysql

// If 'client' variable doesn't exist
if (typeof client === 'undefined') {
  // Connect to the MySQL database
  var client = mysql.createConnection({
    // your connection info
  });

  client.connect()
}

module.exports.handler = (event, context, callback) => {
  // This will allow us to freeze open connections to a database
  context.callbackWaitsForEmptyEventLoop = false;

  client.query('SELECT * FROM `books`', function (error, results) {
    callback(null, results)
  });
}

Refer to AWS document and this tutorial

Upvotes: 0

lexfaraday
lexfaraday

Reputation: 76

I found this useful tool that could help you, https://github.com/jeremydaly/serverless-mysql you need just to install a npm module and configure it. Check also the #connection-backoff section.

Upvotes: 3

Related Questions