Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] [WIP] Prevent database race conditions on replicated databases #20445

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ class Connection implements ConnectionInterface
*/
protected $transactions = 0;

/**
* Have there been changes to the database.
*
* @var int
*/
protected $recordsModified = false;

/**
* All of the queries run against the connection.
*
Expand Down Expand Up @@ -392,6 +399,19 @@ protected function getPdoForSelect($useReadPdo = true)
return $useReadPdo ? $this->getReadPdo() : $this->getPdo();
}

/**
* Set if any records have been modified.
*
* @param bool $changes
* @return void
*/
public function setRecordsModified($changes = true)
{
if (! $this->recordsModified) {
$this->recordsModified = $changes;
}
}

/**
* Run an insert statement against the database.
*
Expand Down Expand Up @@ -446,6 +466,8 @@ public function statement($query, $bindings = [])

$this->bindValues($statement, $this->prepareBindings($bindings));

$this->setRecordsModified();

return $statement->execute();
});
}
Expand Down Expand Up @@ -473,7 +495,11 @@ public function affectingStatement($query, $bindings = [])

$statement->execute();

return $statement->rowCount();
$count = $statement->rowCount();

$this->setRecordsModified($count > 0);

return $count;
});
}

Expand All @@ -490,7 +516,11 @@ public function unprepared($query)
return true;
}

return $this->getPdo()->exec($query) === false ? false : true;
$change = ($this->getPdo()->exec($query) === false ? false : true);

$this->setRecordsModified($change);

return $change;
});
}

Expand Down Expand Up @@ -897,6 +927,10 @@ public function getReadPdo()
return $this->getPdo();
}

if ($this->getConfig('sticky') && $this->recordsModified) {
return $this->getPdo();
}

if ($this->readPdo instanceof Closure) {
return $this->readPdo = call_user_func($this->readPdo);
}
Expand Down