#NestJS - ElephantSQL - To many db connections, fix?

18 messages · Page 1 of 1 (latest)

pastel temple
#

Anyone know how to solve db connections to many? It worked fine until I closed the nestjs app. when i restarted I got this error.

[Nest] 20064  - 05/05/2023, 12:58:05 PM     LOG [GraphQLModule] Mapped {/graphql, POST} route +135ms
Error: Error querying the database: db error: FATAL: too many connections for role "fgypyneu"
    at \node_modules\@prisma\client\runtime\index.js:25336:20

Anyone know a workaround?
I've the following prisma.service.ts

import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
  }

  async enableShutdownHooks(app: INestApplication) {
    this.$on('beforeExit', async () => {
      await app.close();
      await this.$disconnect();
    });
  }
}

Update: after waiting alittle while it work fine again. But if i close the app and restart the same error will occur.

[Nest] 22692  - 05/05/2023, 1:02:29 PM     LOG [GraphQLModule] Mapped {/graphql, POST} route +140ms
[Nest] 22692  - 05/05/2023, 1:02:32 PM     LOG [NestApplication] Nest application successfully started +2947ms
tiny stirrup
#

You might need to tune your connection_limit to fit your ElephantSQL plan: https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#recommended-connection-pool-size

ElephantSQL has quite low limit for the free plan (https://www.elephantsql.com/plans.html).

Also, your enableShutdownHooks is different from the recommended: https://docs.nestjs.com/recipes/prisma#use-prisma-client-in-your-nestjs-services

Don't forget that you need to call the enableShutdownHooks in your main.ts: https://docs.nestjs.com/recipes/prisma#issues-with-enableshutdownhooks

pastel temple
#

found the issue

#

had to update prisma

#

no idea why that helped

#

ah i try that too thanks

#

can I ask...

#

i see this

async function bootstrap() {
  ...
  const prismaService = app.get(PrismaService);
  await prismaService.enableShutdownHooks(app)
  ...
}
#

but i use it like this myself.

#
import { Module } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { OrderResolver } from './orders.resolver';
import { OrderService } from './orders.service';

@Module({
  providers: [PrismaService, OrderService, OrderResolver],
})
export class OrdersModule {}
#

can i add it in main.ts and dont have to add it anywhere else?

#

i've it in 29 places 🙂

#

i've to look up that !

#
import { Module } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { UsersResolver } from './users.resolver';
import { UsersService } from './users.service';

@Module({
  providers: [PrismaService, UsersResolver, UsersService],
  exports: [UsersService],
})
export class UsersModule {}
#

...
property 'enableShutdownHooks' does not exist on type 'typeof PrismaService'.
..

pastel temple
#

i guess i have to learn nestjs 2023

smoky aspen
#

i've it in 29 places
Why do you have 29 instances of the Prisma service? 😱

Create a wrapped PrismaModule, export a single PrismaService from that and import the module where PrismaService is needed.

pastel temple
#

mmh, im gonna try something