fix: wrap updateInventory DB writes in a transaction to prevent concurrent lock errors (#109 #110)

This commit is contained in:
dadaloop82
2026-05-20 15:38:34 +00:00
parent a37d97dfcd
commit 66f5a03503
+10 -1
View File
@@ -2252,6 +2252,9 @@ function updateInventory(PDO $db): void {
$fields[] = "updated_at = CURRENT_TIMESTAMP";
$params[] = $id;
// Wrap all writes in a single transaction to avoid concurrent lock failures.
$db->beginTransaction();
try {
$stmt = $db->prepare("UPDATE inventory SET " . implode(', ', $fields) . " WHERE id = ?");
$stmt->execute($params);
@@ -2282,7 +2285,13 @@ function updateInventory(PDO $db): void {
$stmt->execute([$input['package_unit'], $input['package_size'] ?? 0, $input['product_id']]);
}
// Real-time Bring! sync: if quantity changed, keep Bring! in sync immediately
$db->commit();
} catch (Throwable $e) {
if ($db->inTransaction()) $db->rollBack();
throw $e;
}
// Real-time Bring! sync: done after commit so DB lock is not held during HTTP call
if (isset($input['quantity']) && $prevRow && abs((float)$input['quantity'] - (float)$prevRow['quantity']) > 0.001) {
try { bringQuickSyncProduct($db, (int)$prevRow['product_id']); } catch (Throwable $e) {}
}