can I connect to hyperdrive from within
can I connect to hyperdrive from within a worker while using a mysql2 pool? Kysely only supports using mysql connection pools and I'm getting a "the server closed the connection" error


16 Replies
I can take a look. What is your hyperdrive id?
thank you! ab8fb4d69d834aa5a1af4fee1edef91f
It appears that you are not passing through the
env.HYPERDRIVE.database
field, which will be required to connect successfully.ah it's working now thank you! The db has multiple db schemas that I switch between so I wasn't setting a default one with Kysely, but specifying a default with hyperdrive has now fixed it.
Appreciate your help
I'm still hitting max connections to my db within 5 minutes using hyperdrive, I assumed hyperdrive acted as a pool for mysql and wouldn't keep creating lots of connections for 1 or 2 clients
because the CF worker doesn't allow a global
let db
I'm re-creating the mysql connection in my worker code for each db querycontext:

(I've since switched those connection variables to hyperdrive ones)
Are you attempting to reuse connections to hyperdrive across multiple workers? That will probably not work at all. Connections between your worker and hyperdrive, and hyperdrive and your origin are two different things.
I can set the connection limit to whatever max connections is for your origin database if you like, which will keep hyperdrive from opening more than your origin can handle. Hyperdrive will attempt to open as many connections to origin (up to that limit) as necessary to serve worker requests
well it has about 120 max connections my dev DB, but I thought there would be some db connection re-use with hyperdrive otherwise I can't support more than 120 concurrent visitors
unless the issue is hyperdrive trying to allocate more connections than the db allows, and as you say if limiting hyperdrive to the max connections of the server solves that issue then great
how do I set this, I don't see the option in the dashboard hyperdrive settings page
There definitely should be, unless you're holding open a transaction the entire time a user is connected.
This is not user configurable yet, but 120 is well over the maximum number of connections hyperdrive should use anyway. I will go take another look at your traffic and see if I can tell what's going on
I'm currently migrating from Kysely to Drizzle in case it's the way Kysely is hanging onto the connections as you say
What is the exact error that you are hitting?
well initially it was that I couldn't re-use the global
db
variable because it was declared in the global scope and workers were trying to re-use it I assume between isolates
so then I changed the useDB function to always create a new connection instead of re-using but then I hit connection limits on my DB so it would stop responding to new queries
now however it seems I can't switch to a different mysql db schema because I've had to specify a db in the hyperdrive connectionSo just to clarify, are you creating a new connection when the worker starts, or every time you open a request to the database?
Yes, this is expected.
You will need to make separate hyperdrives to talk to different databases
oh so I can't use db queries with joins between 2 tables in 2 different mysql schemas when using hyperdrive?
No, that is not currently possible and not on the roadmap for now. @thomasgauvin (our pm) new use case you might want to be aware of
It sounds like the multiple schema + multiple hyperdrive is what is exhausting the connections, rather than exhausting the connections of an individual Hyperdrive, have you figured this out @Titan ? is cross-schema joins a must-have for you?