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.

Add `$destroy` to the client so it cannot be reused

See original GitHub issue

Problem

It’s currently hard to maintain prisma on a multi-tenant scenario that requires a lot of clients instantiated for several reasons, one of them that caused me a hard time recently, was the fact that the client does not destroy itself when $disconnect is called, not only this, but it can automatically spin up his internal pool when a new request (intentional or not) comes to him ( #12134 )

Suggested solution

Expose a $destroy function on the client for the developer have the option to complety inutilize and clean prisma from the memory, making any other attempt to call the same client result on a error.

const prismaClient = new PrismaClient();

await prismaClient.$destroy() // <- not sure if promise is needed, but probably a good option

await prismaClient.$executeRaw`SELECT 1`
// Error: 'Client destroyed/closed'

Alternatives

This also could be a client option on initialization, perhaps a flag enabling destruction when $disconnect is called?

Additional context

The whole point in this feature is to let the user-developer decide what happens if a disconnected client receives a query request, silently restarting the prisma internal pool is dangerous for the application and the developer should have the tools to control it, destroying the client is one solution for that, this way the developer assures the client will not accept any incoming queries.

In the context of a Tenant application, a leaked client is obviously a reason for an error in the developer code logic, however the current behavior of prisma does not let the developer take control of that, this could even lead to severe consequences on the database, since a small leak in code logic could leave thousands of clients reopen automatically without the developer even notice

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
itsravenouscommented, Mar 11, 2022

I would love to see this too. My use case might be a bit niche, but I’m currently spinning up a new PrismaClient between jest tests because I destroy and re-migrate the DB between tests to prevent state leaking, and I get the β€œmore than 10 prisma clients are already running” warning. I don’t think it’s actually a problem as it’s only while the tests run, but in case Prisma is more aggressive about this in future I’d like my tests not to break.

I can’t re-use the same PrismaClient across all tests because it fails to connect once the DB has been re-created.

Ideally I’d be able to destroy each PrismaClient after each test.

This is my test setup file (provided to jest’s setupFilesAfterEnv). If there’s an easier/more idiomatic way of achieving what I’m trying to do, I’m all ears 🐘 . I’m aware my approach is different to the official integration test recipe, in that it clears data between each test, rather than each test suite.

let prisma: PrismaClient
beforeEach(async () => {
  prisma = new PrismaClient();
  // Create the database tables as defined in schema.prisma
  execSync('npx prisma db push', {
    env: {
      ...process.env,
      DATABASE_URL: testDatabaseUrl,
    },
  });

  // Seed test users with various roles
  await prisma.user.createMany({
    data: testUsersDb,
  });
});

afterEach(() => {
  // πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘
  // Ideally call prisma.$destroy() here. Have tried prisma.$disconnect() but doesn't seem to
  // squish the warning - i.e. the client is still running even though disconnected
  // πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘

  // Destroy the DB to prevent leaking data between tests
  execSync('npx prisma migrate reset --force', {
    env: {
      ...process.env,
      DATABASE_URL: testDatabaseUrl,
    },
  });
});
2reactions
diovannaschellcommented, Mar 12, 2022

I also would appreciate this a lot! I need at least some way to remove the PrismaClient references from memory, in the current scenario when a client is $disconnected it remains somewhere in the Prisma code, after 10 instances are created, prisma emits a warning saying that there already are 10 instances running, the thing is that I DO NOT want them to be running anynmore, I really need a way to completly destroy the client so I can safely create new ones later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reuse UdpClient vs Disposing it - Stack Overflow
So the main problem is sending those clients a message. The server has no problem receiving messages. This is my protocol (rules):. UdpServerΒ ......
Read more >
Case Manager 5.3.1 - Adding and reusing properties - IBM
From the solution home page, open the Properties page. Then, click Add Property and select either Reuse Property or New. Important: You can...
Read more >
Application Load Balancers - AWS Documentation
If the value is append , the Application Load Balancer adds the client IP address (of ... If you enable HTTP keep-alive, the...
Read more >
Sources and customers | Stripe Documentation
If you need to remove a source from a particular Customer object, you can detach the source. Doing so changes the source's status...
Read more >
Azure Key Vault soft-delete overview - Microsoft Learn
All the other operations will fail. Therefore, even though the object exists, no operations can be performed and hence no usage will occur,...
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