Skip to content

Commit 4f9753b

Browse files
authored
Remove calls to sql.end() in Hyperdrive examples (cloudflare#24429)
As discussed, the workers runtime already handles closing outgoing connections as soon as a worker invocation finishes. And we have reason to believe that sql.end() can cause hangs due to a bug in postgres.js, so it's better to just not include it. I guess I didn't have to remove it from our node-postgres examples, but it isn't needed there either, so why clutter our examples with extra code?
1 parent 9519cf7 commit 4f9753b

File tree

6 files changed

+55
-83
lines changed

6 files changed

+55
-83
lines changed

src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm.mdx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ export default {
8787
// Sample query to get all users
8888
const allUsers = await db.select().from(users);
8989

90-
// Clean up the connection
91-
ctx.waitUntil(sql.end());
92-
9390
return Response.json(allUsers);
9491
},
9592
} satisfies ExportedHandler<Env>;
@@ -169,4 +166,4 @@ Deploy your Worker.
169166
npx wrangler deploy
170167
```
171168

172-
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />
169+
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />

src/content/docs/hyperdrive/get-started.mdx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ export interface Env {
233233
export default {
234234
async fetch(request, env, ctx): Promise<Response> {
235235
// Create a client using the pg driver (or any supported driver, ORM or query builder)
236-
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
237-
const sql = new Client({
238-
connectionString: env.HYPERDRIVE.connectionString,
239-
});
236+
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
237+
const sql = new Client({
238+
connectionString: env.HYPERDRIVE.connectionString,
239+
});
240240

241241
try {
242242
// Connect to the database
@@ -245,9 +245,6 @@ export default {
245245
// Sample query
246246
const results = await sql.query(`SELECT * FROM pg_tables`);
247247

248-
// Clean up the client after the response is returned, before the Worker is killed
249-
ctx.waitUntil(sql.end());
250-
251248
// Return result rows as JSON
252249
return Response.json(results.rows);
253250
} catch (e) {

src/content/docs/hyperdrive/index.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ export default {
5353
try {
5454
// Sample SQL query
5555
const results = await sql`SELECT * FROM pg_tables`;
56-
57-
// Close the client after the response is returned
58-
ctx.waitUntil(sql.end());
59-
6056
return Response.json(results);
6157
} catch (e) {
6258
return Response.json({ error: e instanceof Error ? e.message : e }, { status: 500 });

src/content/docs/workers/tutorials/postgres/index.mdx

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,18 @@ export default {
206206
const sql = new Client({
207207
connectionString: env.DB_URL,
208208
});
209-
try {
210-
// Connect to the PostgreSQL database
211-
await sql.connect();
212-
213-
// Query the products table
214-
const result = await sql.query("SELECT * FROM products");
215-
216-
// Return the result as JSON
217-
return new Response(JSON.stringify(result.rows), {
218-
headers: {
219-
"Content-Type": "application/json",
220-
},
221-
});
222-
} finally {
223-
// Clean up the client connection
224-
await sql.end();
225-
}
209+
// Connect to the PostgreSQL database
210+
await sql.connect();
211+
212+
// Query the products table
213+
const result = await sql.query("SELECT * FROM products");
214+
215+
// Return the result as JSON
216+
return new Response(JSON.stringify(result.rows), {
217+
headers: {
218+
"Content-Type": "application/json",
219+
},
220+
});
226221
},
227222
} satisfies ExportedHandler<Env>;
228223
```
@@ -260,49 +255,44 @@ export default {
260255
const sql = new Client({
261256
connectionString: env.DB_URL,
262257
});
263-
try {
264-
// Connect to the PostgreSQL database
265-
await sql.connect();
266-
267-
const url = new URL(request.url);
268-
if (request.method === "POST" && url.pathname === "/products") {
269-
// Parse the request's JSON payload
270-
const productData = (await request.json()) as {
271-
name: string;
272-
description: string;
273-
price: number;
274-
};
275-
276-
const name = productData.name,
277-
description = productData.description,
278-
price = productData.price;
279-
280-
// Insert the new product into the products table
281-
const insertResult = await sql.query(
282-
`INSERT INTO products(name, description, price) VALUES($1, $2, $3)
283-
RETURNING *`,
284-
[name, description, price],
285-
);
286-
287-
// Return the inserted row as JSON
288-
return new Response(JSON.stringify(insertResult.rows), {
289-
headers: { "Content-Type": "application/json" },
290-
});
291-
}
292-
293-
// Query the products table
294-
const result = await sql.query("SELECT * FROM products");
295-
296-
// Return the result as JSON
297-
return new Response(JSON.stringify(result.rows), {
298-
headers: {
299-
"Content-Type": "application/json",
300-
},
301-
});
302-
} finally {
303-
// Clean up the client connection
304-
await sql.end();
305-
}
258+
// Connect to the PostgreSQL database
259+
await sql.connect();
260+
261+
const url = new URL(request.url);
262+
if (request.method === "POST" && url.pathname === "/products") {
263+
// Parse the request's JSON payload
264+
const productData = (await request.json()) as {
265+
name: string;
266+
description: string;
267+
price: number;
268+
};
269+
270+
const name = productData.name,
271+
description = productData.description,
272+
price = productData.price;
273+
274+
// Insert the new product into the products table
275+
const insertResult = await sql.query(
276+
`INSERT INTO products(name, description, price) VALUES($1, $2, $3)
277+
RETURNING *`,
278+
[name, description, price],
279+
);
280+
281+
// Return the inserted row as JSON
282+
return new Response(JSON.stringify(insertResult.rows), {
283+
headers: { "Content-Type": "application/json" },
284+
});
285+
}
286+
287+
// Query the products table
288+
const result = await sql.query("SELECT * FROM products");
289+
290+
// Return the result as JSON
291+
return new Response(JSON.stringify(result.rows), {
292+
headers: {
293+
"Content-Type": "application/json",
294+
},
295+
});
306296
},
307297
} satisfies ExportedHandler<Env>;
308298
```

src/content/partials/hyperdrive/use-postgres-js-to-make-query.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ export default {
4141
// A very simple test query
4242
const result = await sql`select * from pg_tables`;
4343

44-
// Clean up the client, ensuring we don't kill the worker before that is
45-
// completed.
46-
ctx.waitUntil(sql.end());
47-
4844
// Return result rows as JSON
4945
return Response.json({ success: true, result: result });
5046
} catch (e: any) {

src/content/partials/prompts/base-prompt.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,6 @@ const sql = postgres(env.HYPERDRIVE.connectionString)
541541
// Test query
542542
const results = await sql`SELECT * FROM pg_tables`;
543543

544-
// Clean up the client, ensuring we don't kill the worker before that is
545-
// completed.
546-
ctx.waitUntil(sql.end());
547-
548544
// Return result rows as JSON
549545
return Response.json(results);
550546
} catch (e) {

0 commit comments

Comments
 (0)