Skip to content

Add client cleanup to Hyperdrive examples#28121

Merged
elithrar merged 3 commits intoproductionfrom
opencode/issue27109-20260205212545
Feb 6, 2026
Merged

Add client cleanup to Hyperdrive examples#28121
elithrar merged 3 commits intoproductionfrom
opencode/issue27109-20260205212545

Conversation

@ask-bonk
Copy link
Contributor

@ask-bonk ask-bonk bot commented Feb 5, 2026

Here's a summary of all changes made:

Validation

The issue author is partially correct. The claim that missing .end() calls leak database connections is not quite accurate -- the connection lifecycle docs confirm that Workers automatically garbage collect client connections after each invocation, and Hyperdrive manages the origin pool independently. However, explicitly calling .end() via ctx.waitUntil() is still best practice for clean teardown, and the inconsistency between MySQL examples (which had it) and PostgreSQL examples (which didn't) was genuinely confusing.

Changes Made (10 files, 110 insertions, 18 deletions)

Core fix: Added ctx.waitUntil(client.end()) to all code examples

File Change
get-started.mdx Added ctx.waitUntil(sql.end()) to PostgreSQL example; improved MySQL comment consistency
use-node-postgres-to-make-query.mdx Added ctx.waitUntil(client.end())
use-postgres-js-to-make-query.mdx Added ctx.waitUntil(sql.end())
index.mdx Added cleanup to both PostgreSQL and MySQL overview examples
timescale tutorial Added ctx.waitUntil(client.end()) to both POST and GET routes
drizzle-orm.mdx (Postgres) Added ctx.waitUntil(sql.end())
drizzle-orm.mdx (MySQL) Added ctx.waitUntil(connection.end()), fixed import to mysql2/promise
prisma-orm.mdx Added ctx.waitUntil(prisma.$disconnect())

Key documentation improvement

connection-lifecycle.mdx: Added a new "Cleaning up client connections" section with:

  • Explanation that GC handles cleanup automatically, but .end() is best practice
  • Complete code example showing the pattern
  • A note clarifying that .end() only closes the local client -- not the pooled origin connection

Consistent commenting pattern

All examples now use consistent comments explaining:

  1. "Create a new client on each request. Hyperdrive maintains the underlying database connection pool, so creating a new client is fast."
  2. "Clean up the client after the response is returned. Hyperdrive keeps the underlying connection open in its pool."

Pre-existing bug fixes (found during review)

  • use-node-postgres-to-make-query.mdx: Missing return before new Response(...) in catch block
  • use-mysql2-to-make-query.mdx: Missing error response return in catch block
  • drizzle-orm.mdx (MySQL): Import changed from "mysql2" to "mysql2/promise" for correct Promise-based .end() behavior

Closes #27109

github run

… code examples

Address the inconsistency where MySQL examples included ctx.waitUntil(connection.end())
but all PostgreSQL examples (node-postgres, postgres.js, Drizzle, Prisma, Timescale tutorial)
omitted client cleanup entirely. This adds:

- ctx.waitUntil(client.end()) to all PostgreSQL code examples
- ctx.waitUntil(connection.end()) to MySQL Drizzle ORM example
- ctx.waitUntil(prisma.$disconnect()) to Prisma ORM example
- A new 'Cleaning up client connections' section in connection-lifecycle.mdx
  explaining the best practice and clarifying that .end() only closes the
  local client, not the Hyperdrive-managed pool connection
- Consistent comments across all examples explaining that Hyperdrive keeps
  the underlying database connection open in its pool for reuse
- Updated step-by-step explanations in get-started.mdx for both PG and MySQL

Also fixes pre-existing bugs:
- Missing 'return' in node-postgres partial catch block
- Missing error response in mysql2 partial catch block
- Changed mysql2 Drizzle ORM import from 'mysql2' to 'mysql2/promise' for
  correct Promise-based .end() behavior

Closes #19988
@ask-bonk ask-bonk bot requested review from a team, Oxyjun, elithrar, sejoker and thomasgauvin as code owners February 5, 2026 21:42
@github-actions github-actions bot added product:hyperdrive Hyperdrive: https://developers.cloudflare.com/hyperdrive/ size/m labels Feb 5, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

This PR requires additional review attention because it affects the following areas:

Partials

This PR updates partial files, which are pieces of content used across multiple files in our Render component.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/hyperdrive/ @elithrar, @thomasgauvin, @sejoker, @oxyjun, @cloudflare/pcx-technical-writing
/src/content/partials/hyperdrive/ @elithrar, @thomasgauvin, @sejoker, @oxyjun, @cloudflare/pcx-technical-writing

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

Preview URL: https://f9e2cc4e.preview.developers.cloudflare.com
Preview Branch URL: https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com

Files with changes (up to 15)

Original Link Updated Link
https://developers.cloudflare.com/hyperdrive/concepts/connection-lifecycle/ https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com/hyperdrive/concepts/connection-lifecycle/
https://developers.cloudflare.com/hyperdrive/get-started/ https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com/hyperdrive/get-started/
https://developers.cloudflare.com/hyperdrive/ https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com/hyperdrive/
https://developers.cloudflare.com/hyperdrive/tutorials/serverless-timeseries-api-with-timescale/ https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com/hyperdrive/tutorials/serverless-timeseries-api-with-timescale/
https://developers.cloudflare.com/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/drizzle-orm/ https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/drizzle-orm/
https://developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm/ https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm/
https://developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/prisma-orm/ https://opencode-issue27109-20260205212545.preview.developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/prisma-orm/

@elithrar
Copy link
Collaborator

elithrar commented Feb 6, 2026

/bonk resolve the conflicts and re-review

@ask-bonk
Copy link
Contributor Author

ask-bonk bot commented Feb 6, 2026

To https://github.com/cloudflare/cloudflare-docs
! [rejected] opencode/issue27109-20260205212545 -> opencode/issue27109-20260205212545 (non-fast-forward)
error: failed to push some refs to 'https://github.com/cloudflare/cloudflare-docs'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

github run

@ask-bonk
Copy link
Contributor Author

ask-bonk bot commented Feb 6, 2026

Bonk workflow failed. Check the logs for details.

View workflow run

@elithrar
Copy link
Collaborator

elithrar commented Feb 6, 2026

/bonk re-review again. Fix any merge markers. Make sure all code examples are fixed.

…e and missing cleanup in index.mdx PostgreSQL example
@ask-bonk
Copy link
Contributor Author

ask-bonk bot commented Feb 6, 2026

Done. Here's a summary of the review and fixes:

Review Results

No merge conflict markers found in any of the 10 changed files.

Two code example bugs found and fixed (both caused by the merge with PR #28119 which changed Postgres examples from postgres driver to pg driver):

  1. drizzle-orm.mdx (Postgres) at line 92: ctx.waitUntil(sql.end()) referenced the old sql variable from the postgres driver, but after the merge the variable is now client (from the pg driver). Fixed to ctx.waitUntil(client.end()).

  2. index.mdx (PostgreSQL tab): The overview PostgreSQL example was rewritten by PR Use pg in Hyperdrive Postgres examples #28119 to use the pg driver, but the ctx.waitUntil(client.end()) cleanup call and the updated comment were lost during merge resolution. Added both back.

All other 8 files are correct - the cleanup calls, comments, error handling fixes, and mysql2/promise import change all look good after the merge.

github run

@elithrar elithrar enabled auto-merge (squash) February 6, 2026 12:31
@elithrar elithrar merged commit 4bd1d0b into production Feb 6, 2026
9 checks passed
@elithrar elithrar deleted the opencode/issue27109-20260205212545 branch February 6, 2026 12:40
@elithrar

This comment was marked as off-topic.

@ask-bonk
Copy link
Contributor Author

ask-bonk bot commented Feb 6, 2026

fatal: couldn't find remote ref opencode/issue27109-20260205212545

github run

@ask-bonk
Copy link
Contributor Author

ask-bonk bot commented Feb 6, 2026

Bonk workflow failed. Check the logs for details.

View workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:hyperdrive Hyperdrive: https://developers.cloudflare.com/hyperdrive/ size/m

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hyperdrive Getting Started guide should address connection clean-up

4 participants