Use locking for shift transacton id generation

This commit is contained in:
Igor Scheller 2022-06-12 14:37:58 +02:00
parent 96277dcfc4
commit 7c448e6064
2 changed files with 14 additions and 2 deletions

View File

@ -559,9 +559,14 @@ function Shift_update($shift)
/** /**
* Get the next free shifts transaction id * Get the next free shifts transaction id
*/ */
function Shift_get_next_transaction_id(): int function Shift_get_next_transaction_id($lock = true): int
{ {
return Db::selectOne('SELECT MAX(transaction_id) + 1 AS transaction_id FROM Shifts')['transaction_id'] ?? 1; $query = Db::connection()->table('Shifts')->select('transaction_id');
if ($lock) {
$query->lockForUpdate();
}
return $query->max('transaction_id') + 1;
} }
/** /**

View File

@ -365,10 +365,17 @@ function admin_shifts()
throw_redirect(page_link_to('admin_shifts')); throw_redirect(page_link_to('admin_shifts'));
} }
$transactionRunning = true;
Db::connection()->beginTransaction();
$transactionId = Shift_get_next_transaction_id(); $transactionId = Shift_get_next_transaction_id();
foreach ($session->get('admin_shifts_shifts', []) as $shift) { foreach ($session->get('admin_shifts_shifts', []) as $shift) {
$shift['URL'] = null; $shift['URL'] = null;
$shift_id = Shift_create($shift, $transactionId); $shift_id = Shift_create($shift, $transactionId);
if ($transactionRunning) {
$transactionRunning = false;
Db::connection()->commit();
}
engelsystem_log( engelsystem_log(
'Shift created: ' . $shifttypes[$shift['shifttype_id']] 'Shift created: ' . $shifttypes[$shift['shifttype_id']]