From a424b1ed9202495728f43048fc7ee05bb4bcb7fc Mon Sep 17 00:00:00 2001 From: paula-alves Date: Fri, 6 Nov 2026 21:14:06 +0000 Subject: [PATCH] fix(sports): give implicit upserts enough time to grab a connection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SportsService.refreshFpa (and other plain upsert() calls across the service) opened under brief pool contention and hit "Transaction API error: Unable to start a transaction in the given time" — Prisma opens an implicit transaction around a single upsert() and only gives it its default maxWait (2000ms) to grab a pooled connection, same failure mode already fixed for explicit $transaction() calls in rebuildDirectory and library/events.ts. Set a client-wide transactionOptions default in createDb so any call that does not set its own maxWait/timeout gets 10s instead of the Prisma default; call sites with their own long-running $transaction options keep overriding it. --- apps/api/src/db.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/api/src/db.ts b/apps/api/src/db.ts index 336fc70..16d85fe 100644 --- a/apps/api/src/db.ts +++ b/apps/api/src/db.ts @@ -3,7 +3,17 @@ import { PrismaClient } from "./generated/prisma/client.js"; export type Db = PrismaClient; -/** `connections`: the pool's size (node-postgres keeps 10 by default); the worker sizes it to what it runs at once. */ +/** + * `connections`: the pool's size (node-postgres keeps 10 by default); the worker sizes it to what it runs at once. + * + * `transactionOptions` raises Prisma's default `maxWait` (2000ms) for any call that doesn't set its own — + * including the implicit transaction Prisma opens around a single `upsert()` — so brief pool contention + * doesn't surface as "Unable to start a transaction in the given time". Call sites with their own long-running + * `$transaction(fn, { maxWait, timeout })` options (e.g. `rebuildDirectory`, `assignGalleries`) override this. + */ export function createDb(databaseUrl: string, connections?: number): Db { - return new PrismaClient({ adapter: new PrismaPg({ connectionString: databaseUrl, ...(connections && { max: connections }) }) }); + return new PrismaClient({ + adapter: new PrismaPg({ connectionString: databaseUrl, ...(connections && { max: connections }) }), + transactionOptions: { maxWait: 10_000, timeout: 10_000 }, + }); } -- 2.47.3