question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection pool issue when deploying to Google Cloud Run/Cloud SQL

See original GitHub issue

Bug 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:

  1. 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)
    
  2. Build and deploy through docker
  3. 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

  1. 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
shwethashyam1commented, Feb 19, 2021

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.

0reactions
janpiocommented, Feb 19, 2021

Nice. Keep us updated 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found