I’m using Prisma python client for my mysql database. I’m wondering if its possible to do a single global connection instead of having to open and close them whenever I make a query?
JavaScript
x
10
10
1
@commands.has_permissions(manage_guild=True)
2
@commands.slash_command(description="Sets up the server for verification")
3
async def help(self, interaction: disnake.GuildCommandInteraction):
4
await self.client.prisma.connect()
5
response = await self.client.prisma.user.find_first(where={
6
"name": "benn",
7
})
8
print(dict(response))
9
await self.client.prisma.disconnect()
10
I tried doing the following:
JavaScript
1
6
1
client.prisma = Prisma(auto_register=True)
2
async def connect_to_db():
3
await client.prisma.connect()
4
5
asyncio.run(connect_to_db())
6
However, I get an error:
JavaScript
1
2
1
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: Event loop is closed```
2
Advertisement
Answer
It is recommended that you create one instance of PrismaClient
and reuse it across your application and you should only set it to a global variable in the development environment only and you do not need to explicitly $disconnect
. You can learn more about Prisma connection management in the docs. Also, I’ll encourage you to ask your Prisma Python client questions in prisma-client-py repositories GitHub Discussion