Skip to content

Commit

Permalink
Merge pull request #21 from makise-co/feature/pq-concurrent-statement…
Browse files Browse the repository at this point in the history
…s-fix

Revert concurrency statement handling
  • Loading branch information
codercms authored Oct 8, 2021
2 parents 34eb4b6 + 994ae4c commit 814f1a2
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/Driver/Pq/PqHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,38 @@ public function prepare(string $sql): Statement

$name = Handle::STATEMENT_NAME_PREFIX . sha1($modifiedSql);

while (isset($this->statements[$name])) {
// prevent returning of promised statement which is in de-allocation state
if (isset($this->statements[$name]) && !$this->statements[$name]->isDeallocating) {
$storage = $this->statements[$name];

++$storage->refCount;

// Statement may be being allocated or deallocated. Wait to finish, then check for existence again.
if ($storage->lock->isLocked()) {
// if statement is in allocation state we should wait until allocation done
if ($storage->isAllocating) {
// Do not return promised prepared statement object, as the $names array may differ.
$storage->lock->wait();
--$storage->refCount;
continue;

if ($storage->error !== null) {
throw $storage->error;
}
}

return new PqStatement($this, $name, $sql, $names);
}

// wait for statement complete de-allocation, before allocating new one
if (isset($this->statements[$name]) && $this->statements[$name]->isDeallocating) {
$this->statements[$name]->lock->wait();
}

$storage = new StatementStorage();
$storage->sql = $sql;
$storage->lock->lock();
$storage->isAllocating = true;

$this->statements[$name] = $storage;

$storage->lock->lock();
$storage->isAllocating = true;

try {
$storage->statement = $this->send(
$sql,
Expand Down

0 comments on commit 814f1a2

Please sign in to comment.