Hi all,
Hi all,
Do we need to create a new SQL connection for every request with Hyperdrive and close it after fetching the data, or should we create a single connection instance and reuse it? The latter approach seems more reasonable to me but it doesn't appear to work on Workers.
6 Replies
In general, Hyperdrive expects that you will create the new connection every time. Hyperdrive already maintains a warm pool of connections to your origin database that it will use to service the requests you make from your worker
Thank you for your response. Would you recommend closing the SQL connections manually after each request as well?
Also could you please advise whether the following configuration for establishing SQL connections to Hyperdrive is appropriate to use across the worker? I’ve seen some examples where max is set to 1 instead of 5.
I think all of the options you've mentioned should be fine. I typically close the sql connections out of habit, but they will be severed once your worker completes regardless.
Maybe somebody else knows more about this, but I'm not aware of any issues with having either 1 or >1 max connections
Sometimes my SQL queries fail randomly and I get the following error:
I thought it might be due to not closing the Hyperdrive connection correctly or its configurations but I think it might be a problem with Hyperdrive itself
That's so interesting. Can you share the actual queries you're using? In DM is fine, if you prefer. I was just seeing those errors come in over the weekend and haven't seen them before.
I'm also going to try running our test with those settings, to see if one of them is behaving in a way I wouldn't expect
If you can get away with it, please also consider
fetch_types: false
. It'll save you a lot of overhead if you can get away without it.
I just ran through a large test suite with those settings and they worked correctly, so I think the next step would be to share Hyperdrive ID and example queries, if you would.I DMed you.