Connection pool issue when deploying to Google Cloud Run/Cloud SQL
See original GitHub issueBug description
This error happens when I run my scheduler functions on my express app, every minute, and deploy it to GCP:
PrismaClientInitializationError2 [PrismaClientInitializationError]: Timed out fetching a new connection from the pool. Please consider reducing the number of requests or increasing the `connection_limit` parameter (https://www.prisma.io/docs/concepts/components/prisma-client/connection-management#connection-pool). Current limit: 20.
How to reproduce
Steps to reproduce the behavior:
- Add a scheduler function to an express server:
cron.schedule('* * * * *', () => { await updateDB.updateIsExpiredColumn(); (// updateDB reference a .js which calls a prisma instance from prisma client) - Build and deploy through docker
- On psql connection string, call out connection limit parameter explicitly to 20. In addition, I also have a 4-CPU set up on GCP where this is running. PSQL connection string: postgres://username:password@publicIP/dbname?connection_limit=20&host=/cloudsql/my-project-id:us-central1:production
Expected behavior
- scheduler function should run every minute and should not have connection pool issues
Prisma information
prisma schema –
provider = "prisma-client-js"
output = "../functions/models/prisma-client"
binaryTargets = ["native", "debian-openssl-1.1.x"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
updateDB.js –
const prisma = require('./prismaClient');
const updateIsExpiredColumn = async() => {
const currentTimeISO = new Date().toISOString();
const twoHoursPriorTimestamp = Date.now() - (60000*60*2);
const twoHoursPriorISOString = new Date(twoHoursPriorTimestamp);
console.log('current time ====', currentTimeISO);
console.log('two hours prior ====', twoHoursPriorISOString);
await prisma.caravansmsinstance.updateMany({
where: {
createdat: {
lte: twoHoursPriorISOString,
},
AND: {
isTwilioNoExpired: {
equals: false,
},
},
},
data: {
isTwilioNoExpired: {
set: true,
},
},
});
await prisma.$disconnect();
}
prismaClient.js –
const { PrismaClient } = require('./prisma-client');
const url = process.env.DATABASE_URL;
let prisma;
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient({
datasources: { db: { url } },
})
} else {
if (!global.prisma) {
global.prisma = new PrismaClient({
datasources: { db: { url } },
})
}
prisma = global.prisma
}
module.exports = prisma;
Environment & setup
- OS: Debian
- Database: PostgreSQL
- Node.js version: runtime: nodejs12 (Dockerfile)
- Prisma version: 2.16.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Connect from Cloud Run | Cloud SQL for MySQL
Click on the service name. Click on the Connections tab. Click Deploy. Enable connecting to a Cloud SQL instance: Note: If your application...
Read more >how to manage Cloud SQL connection pool on Cloud Run
I need to manage Cloud SQL max_connections, to prevent too many connecitons error. So how can I manage connections on Cloud Run? Support...
Read more >Cloud Run to Cloud SQL Connection stops working af...
java.lang.RuntimeException: [ourproject:region:instance] Failed to update metadata for Cloud SQL instance. at com.google.cloud.sql.
Read more >Google Cloud SQL Auth Proxy demystified
It is important to note that the proxy will only create one connection to the Cloud SQL instance and does not provide connection...
Read more >Cloud SQL with private IP only: the Good, the Bad and ...
Cloud SQL service, the managed database service on Google Cloud, allows you to: Set a private IP on your instance and to connect...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks @janpio. I am not seeing this error anymore. I also deployed my scheduled functions to google cloud scheduler, so that appears to be working too.
Nice. Keep us updated 👍