Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you use transactions with a Pool? #411

Open
jenders-polar opened this issue Nov 15, 2024 · 0 comments
Open

How do you use transactions with a Pool? #411

jenders-polar opened this issue Nov 15, 2024 · 0 comments

Comments

@jenders-polar
Copy link

Below is a snippet of code I am trying to get working... basically I get the pool and call connect on it to get a single connection.

I call beginTransaction on the connection, run my statements, then call commit or rollback.

The problem with this, is that after 3 calls to this function, the pool stops working and doesn't respond to anything.

Am I doing something wrong here? Should I not be using a pool to do transactions?

export async function executeStatements(statements: Statement[]) {
  const pool = await getPool();

  const connection = await pool.connect();
  await connection.beginTransaction();

  try {
    const results = statements.map((statement) =>
      connection.query(
        statement.sql,
        statement.parameters as Array<number | string>,
      ),
    );
    (await Promise.allSettled(results)).forEach((promise) => {
      if (promise.status === "rejected") {
        throw Error();
      }
    });
    await connection.commit();
  } catch (error) {
    await connection.rollback();
  }

  return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant