Add `$destroy` to the client so it cannot be reused
See original GitHub issueProblem
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:
- Created 2 years ago
- Reactions:6
- Comments:7 (2 by maintainers)
Top GitHub Comments
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.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
$disconnect
ed 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.