Skip to content

Commit 20e1028

Browse files
committed
More description and use 1 second after-generate lock.
1 parent 9c10c3a commit 20e1028

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/VariableGenerator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,25 @@ public function __construct(
3535
/**
3636
* Generate new variable symbol by last variable.
3737
* In case of invalid last symbol or init, use first valid symbol by specific strategy.
38+
* You must use the generated ID immediately to save the new record to the database.
39+
* Before generating the ID, a lock (transaction) is created, and we reserve a time of 1000 ms to save new entity.
40+
* After that, the next number may be generated and data integrity may be broken.
3841
*/
3942
public function generate(?string $last = null, string $transactionName = 'variable-generator'): int
4043
{
4144
Lock::wait($transactionName);
42-
Lock::startTransaction($transactionName);
45+
Lock::startTransaction($transactionName, 15000);
4346
$last ??= $this->variableLoader->getCurrent();
4447
$new = $last === null
4548
? $this->strategy->getFirst()
4649
: $this->strategy->generate((string) preg_replace('/\D+/', '', $last));
4750

48-
return (int) $new;
51+
$return = (int) $new;
52+
53+
// Starts a short transaction because the new method must be saved (provision for application and database delays).
54+
Lock::startTransaction($transactionName, 1000);
55+
56+
return $return;
4957
}
5058

5159

src/VariableLoader.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@
1212
*/
1313
interface VariableLoader
1414
{
15+
/**
16+
* Returns the currently most recent ID or entity number for which we will generate a new identifier.
17+
* Always retrieve real data for generation (for example, by calling an SQL query) and do not use caching.
18+
* Always try to optimize the data retrieval processing as much as possible,
19+
* as this method cannot be called in parallel and is disk locked during processing
20+
* preventing other processes from generating the new ID.
21+
*/
1522
public function getCurrent(): ?string;
1623
}

0 commit comments

Comments
 (0)