Reusing db connection in lambda with Drizzle

I'm using rds mysql community as my DB and have been running into a connection limit problem when using server actions in Nextjs with prisma to interact with it(first image is the number of connections when I was burst testing), after searching a bit through the sst discord saw that someone recommended drizzle for better perfomance. When I looked through their docs I saw they mentioned a way to reuse the db connection: https://orm.drizzle.team/docs/perf-serverless
But through their example I found it hard to understand how it's being shared and if that would help with my connection limit problem

My connection looks like this:
const connection = await mysql.createConnection({
  host: env.MYSQL_HOST,
  user: env.MYSQL_USER,
  port: 3306,
  password: env.MYSQL_PASSWORD,
  database: env.MYSQL_DATABASE,
});

export const db = drizzle(connection, {
  schema: myschema,
  mode: "default",
});

And this is an example server action
"use server";

import { db } from "@/server/db";

export const getPosts = async () => {
  const posts = await db.query.posts.findMany();
  return posts;
};

Is there anything else I need to do so it can reuse the connection? Because just letting it like this isn't really working as you can see in the second image.
image.png
image.png
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Was this page helpful?