From cb425fb3971b200d450936d201c6fc73a55dbfc9 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 26 Apr 2026 12:14:17 +0200 Subject: [PATCH] Fix 500 on reservation edit after DB reinit (issue #883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit saveEndpoints was bound at module load via db.transaction(...). When the demo-mode hourly reset (or a self-hoster's backup restore) closes the DB connection and reinitialises it, the bound transaction still references the now-closed connection — every subsequent reservation save with an endpoints field throws "The database connection is not open", which the client surfaces as "Internal server error". Bind the transaction lazily on each call so it always runs against the current connection. --- server/src/services/reservationService.ts | 26 +++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/server/src/services/reservationService.ts b/server/src/services/reservationService.ts index d95aadd5..354a14f7 100644 --- a/server/src/services/reservationService.ts +++ b/server/src/services/reservationService.ts @@ -61,16 +61,24 @@ function resolveDayIdFromTime( return row?.id ?? null; } -const saveEndpoints = db.transaction((reservationId: number, endpoints: EndpointInput[]) => { - db.prepare('DELETE FROM reservation_endpoints WHERE reservation_id = ?').run(reservationId); - const insert = db.prepare(` - INSERT INTO reservation_endpoints (reservation_id, role, sequence, name, code, lat, lng, timezone, local_time, local_date) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `); - endpoints.forEach((e, i) => { - insert.run(reservationId, e.role, e.sequence ?? i, e.name, e.code ?? null, e.lat, e.lng, e.timezone ?? null, e.local_time ?? null, e.local_date ?? null); +function saveEndpoints(reservationId: number, endpoints: EndpointInput[]): void { + // Bind the transaction lazily on each call. Binding at module load time + // captures the DB connection that was open then, which becomes invalid + // after demo-reset / restore-from-backup closes and reinitialises the + // connection — every later endpoint save would throw + // "The database connection is not open". + const tx = db.transaction((rid: number, eps: EndpointInput[]) => { + db.prepare('DELETE FROM reservation_endpoints WHERE reservation_id = ?').run(rid); + const insert = db.prepare(` + INSERT INTO reservation_endpoints (reservation_id, role, sequence, name, code, lat, lng, timezone, local_time, local_date) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + `); + eps.forEach((e, i) => { + insert.run(rid, e.role, e.sequence ?? i, e.name, e.code ?? null, e.lat, e.lng, e.timezone ?? null, e.local_time ?? null, e.local_date ?? null); + }); }); -}); + tx(reservationId, endpoints); +} export function listReservations(tripId: string | number) { const reservations = db.prepare(`