Disconnect for Lambda? #2221
-
The Prisma docs mention that it is recommended to always explicitly call prisma.disconnect() in our code. Your quickstart example shows that, but when I checked out your aws lambda example, it didn't show it. https://github.com/prisma/prisma-examples/tree/master/deployment-platforms/aws-lambda We're deploying to AWS lambda and I wanted to double check with you what we should do. We're actually using graphql-yoga server in our lambda function, so it'd be wonderful to have an example for how to add this to the server invocation in some way. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hey @jonthewayne, That recommendation applies in the context of a long-running Node.js process that uses Prisma Client. In the context of AWS Lambda, and more broadly serverless environment, it's ok not disconnect so that further invocations of the Serverless function can reuse the DB connection. It's worth noting that reusing the connection, i.e. omitting the disconnect call, in Serverless functions has its limits, as it can exhaust the connection pool depending on your traffic pattern and concurrency limit (the number of serverless functions that can be invoked simultaneously). For a more elaborate explanation of the considerations, check out the Serverless deployment docs |
Beta Was this translation helpful? Give feedback.
-
I can confirm that when using Graphql Yoga server, and passing in I tried creating a middleware that would call |
Beta Was this translation helpful? Give feedback.
-
The fix for this, as detailed in #2241 (comment) is to set the the exports.server = (event, context, cb) => {
// Set to false to send the response right away when the callback executes, instead of waiting for the Node.js event loop to be empty.
context.callbackWaitsForEmptyEventLoop = false
return lambda.graphqlHandler(event, context, cb)
} |
Beta Was this translation helpful? Give feedback.
The fix for this, as detailed in #2241 (comment) is to set the the
context.callbackWaitsForEmptyEventLoop
of the AWS Lambda Context Object tofalse
as follows: