In my lambda function, I tried closing the mongo connection as soon as I send the callback. But this is a problem.
- When I submit a request, the function performs its duties, sends a callback and closes the database connection.
- When sending a second request, the function is disabled.
- When I delete
db.close(), everything works fine.
I think lambda reuses the connection for all functions, because I open the connection at the top of the handler:
mongoose.connect(process.env.DATABASE_URL);
const handleCreateUser = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const data = JSON.parse(event.body);
const { user, userProfile } = data;
await createUser({ callback, user, userProfile });
};
Any idea how to fix this? Do we really need to close the connection at this point?
source
share