Ahmar Suhail
06/24/2021, 10:40 AMTimed out fetching a new connection from the connection pool
. I have a couple of q’s around this:
• Is this error only thrown when prisma client tries to get a connection from the pool to run a query? Or is also thrown when the DB has reached it’s max connection limit and the prisma client isn’t able to reserve connections to create a connection pool?
• If it’s only thrown in the first case, what could be the possible reasons for it to be thrown when all queries are executed sequentially and connection pool size is 5?
• We get a prisma client by doing ->
const getClient = async () => {
// get connection string stored in AWS parameter store
return new PrismaClient()
}
And then we call getClient
in our handler. So basically anytime a handler is called a new prisma client is initialised. What does this mean in terms of the connection pool? Will prisma create a new pool every time we do new PrismaClient()
?Ryan
06/24/2021, 10:47 AMPrismaClient
outside the handler as each PrismaClient
instance creates a new connection pool.Ryan
06/24/2021, 10:48 AMPrismaClient
instance should look something like this: https://github.com/ryands17/graphql-api-cdk-serverless-postgres/blob/main/lambda-fns/db.tsAhmar Suhail
06/24/2021, 10:48 AMjanpio
janpio
janpio
Timed out fetching a new connection from the connection pool
is indeed only when the connection pool does not return a connection to the Client in the defined amount of time. The database is not involved here at all.
This is usually the case because the connection pool has a pool size limit (defined via connection_limit
). When all those are in use for running queries, additional queries have to wait - and after x seconds you get the error message you posted above.janpio
connect_timeout
is triggered first (which is half the amount of seconds than pool_timeout
).janpio
What could be the possible reasons for it to be thrown when all queries are executed sequentially and connection pool size is 5?
janpio
janpio
pool_timeout
to a really high number and see what happens.janpio
Ahmar Suhail
06/24/2021, 11:11 AMconnect_timeout
first?Ahmar Suhail
06/24/2021, 11:12 AMhandler = () => {
const prisma = await getDatabaseConnection();
const {
Records: [{ messageAttributes: data }],
} = event;
const { lead } = getPartsFromWebhookData(data);
console.log("Converting lead: ", lead);
const eventRequest = await prisma.event_requests.findUnique({
where: { id: lead.cf_request_id },
});
// Do stuff here
}
Ahmar Suhail
06/24/2021, 11:13 AM{
"errorType": "Error",
"errorMessage": "\nInvalid `prisma.event_requests.findUnique()` invocation:\n\n\n Timed out fetching a new connection from the connection pool. (More info: <http://pris.ly/d/connection-pool>, Current connection limit: 5)",
"clientVersion": "2.24.1",
"stack": [
"Error: ",
"Invalid `prisma.event_requests.findUnique()` invocation:",
"",
"",
" Timed out fetching a new connection from the connection pool. (More info: <http://pris.ly/d/connection-pool>, Current connection limit: 5)",
" at cb (/opt/nodejs/node_modules/@prisma/client/runtime/index.js:35229:17)",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
janpio
connect_timeout
should trigger and give you taht error message.janpio
janpio
prisma.$disconnect()
at the end?janpio
pool_timeout=300
to your connection string and see if it indeed waits that long, and then outputs the pool timeout error message happens again or something else.janpio
Ahmar Suhail
06/24/2021, 11:18 AMprisma.$disconnect()
. I'll also be updating the code to https://github.com/ryands17/graphql-api-cdk-serverless-postgres/blob/main/lambda-fns/db.ts as ryan suggestedAhmar Suhail
06/24/2021, 11:19 AM