Skip to content

PdoSessionPersistence

Default PDO-backed SessionPersistenceInterface implementation.

Works on Postgres, MySQL and SQLite — the upsert is chosen per driver, since no single statement is portable across all three (see buildSaveSql()).

Expects a table with (at least) sess_id/sess_data/sess_time columns, matching the schema most PHP session table conventions already use:

CREATE TABLE session ( sess_id VARCHAR(64) PRIMARY KEY, sess_data BYTEA/BLOB/TEXT NOT NULL, sess_time TIMESTAMP NOT NULL );

class PdoSessionPersistence implements SessionPersistenceInterface

ImplementsSessionPersistenceInterface
SourceSession/PdoSessionPersistence.php

public function __construct(PDO $pdo, array<string, mixed> $parameters = [], SessionCodecInterface $codec = new SessionCodec(…)): mixed

ParameterTypeDescription
$pdoPDO
$parametersarray``<``string``, ``mixed``>
$codecSessionCodecInterface

Returns mixed

MethodDescription
delete(string $sid): voidDeletes the session row.
load(string $sid): ?arraySelects the session row and decodes its payload through the codec.
save(string $sid, array $data): voidUpserts the encoded session payload against the id.

public function delete(string $sid): void

Deletes the session row.

A database failure is logged at error rather than thrown — the caller is usually mid-logout or mid-rotation and has nothing useful to do with the exception — so a failed delete leaves the session loadable until it expires.

ParameterTypeDescription
$sidstring

public function load(string $sid): ?array

Selects the session row and decodes its payload through the codec.

Returns null when the id has no row or the stored blob is empty. A bytea column comes back from pdo_pgsql as a stream resource rather than a string, so the blob is drained before decoding. The cursor is always closed, including on failure: a fetched-but-unclosed statement leaves the cached statement open, which on SQLite holds a shared lock that a later PdoSessionPersistence::save() upsert cannot upgrade.

ParameterTypeDescription
$sidstring

Returns ?``array

ThrowsWhen
StorageExceptionif the query fails.

public function save(string $sid, array $data): void

Upserts the encoded session payload against the id.

The statement is the driver-specific upsert built once per instance, and the payload is bound as a LOB so a binary encoding survives on drivers with a bytea/blob column.

ParameterTypeDescription
$sidstring
$dataarray
ThrowsWhen
StorageExceptionif encoding or the write fails.