I read a few articles on calling rds from lambdas and how to use ‘context.callbackWaitsForEmptyEventLoop = false’ in order to try to hand on to cached connections in warm lambdas.
I have read some that advise to use a connection pool of size 1, but is there even any point in using a conenciton pool. Wouldn’t a simple connection like below serve the exact same purpose ?
import * as dynamoDbLib from "../../libs/dynamodb-lib";
import { success, failure } from "../../libs/response-lib";
var mysql = require('mysql2/promise');
let cachedConnection = null
export const main = async (event, context, callback) => {
console.log('event', JSON.stringify(event))
try {
context.callbackWaitsForEmptyEventLoop = false;
if (cachedConnection === null) {
console.log('creating connection')
cachedConnection = await mysql.createConnection({
host: "dev.xxxx.rds.amazonaws.com",
user: "xxxxxxxx",
password: "xxxxxxxxxxx",
database: "orders-service",
});
console.log('connection created')
} else {
console.log('using cached connection')
}
const [rows, fields] = await cachedConnection.query('insert into test set ?', { orderId: event.id});
callback(null, success(rows));
} catch (error) {
console.log(error)
callback(null, failure({ status: false, error: error }));
}
};