public function LegacyPdoSessionHandler::write in Zircon Profile 8
Same name and namespace in other branches
- 8.0 vendor/symfony/http-foundation/Session/Storage/Handler/LegacyPdoSessionHandler.php \Symfony\Component\HttpFoundation\Session\Storage\Handler\LegacyPdoSessionHandler::write()
File
- vendor/
symfony/ http-foundation/ Session/ Storage/ Handler/ LegacyPdoSessionHandler.php, line 177
Class
- LegacyPdoSessionHandler
- Session handler using a PDO connection to read and write data.
Namespace
Symfony\Component\HttpFoundation\Session\Storage\HandlerCode
public function write($sessionId, $data) {
$encoded = base64_encode($data);
try {
// We use a single MERGE SQL query when supported by the database.
$mergeSql = $this
->getMergeSql();
if (null !== $mergeSql) {
$mergeStmt = $this->pdo
->prepare($mergeSql);
$mergeStmt
->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$mergeStmt
->bindParam(':data', $encoded, \PDO::PARAM_STR);
$mergeStmt
->bindValue(':time', time(), \PDO::PARAM_INT);
$mergeStmt
->execute();
return true;
}
$updateStmt = $this->pdo
->prepare("UPDATE {$this->table} SET {$this->dataCol} = :data, {$this->timeCol} = :time WHERE {$this->idCol} = :id");
$updateStmt
->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$updateStmt
->bindParam(':data', $encoded, \PDO::PARAM_STR);
$updateStmt
->bindValue(':time', time(), \PDO::PARAM_INT);
$updateStmt
->execute();
// When MERGE is not supported, like in Postgres, we have to use this approach that can result in
// duplicate key errors when the same session is written simultaneously. We can just catch such an
// error and re-execute the update. This is similar to a serializable transaction with retry logic
// on serialization failures but without the overhead and without possible false positives due to
// longer gap locking.
if (!$updateStmt
->rowCount()) {
try {
$insertStmt = $this->pdo
->prepare("INSERT INTO {$this->table} ({$this->idCol}, {$this->dataCol}, {$this->timeCol}) VALUES (:id, :data, :time)");
$insertStmt
->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$insertStmt
->bindParam(':data', $encoded, \PDO::PARAM_STR);
$insertStmt
->bindValue(':time', time(), \PDO::PARAM_INT);
$insertStmt
->execute();
} catch (\PDOException $e) {
// Handle integrity violation SQLSTATE 23000 (or a subclass like 23505 in Postgres) for duplicate keys
if (0 === strpos($e
->getCode(), '23')) {
$updateStmt
->execute();
}
else {
throw $e;
}
}
}
} catch (\PDOException $e) {
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e
->getMessage()), 0, $e);
}
return true;
}