PrimaryReadReplicaConnection
extends Connection
in package
Primary-Replica Connection
Connection can be used with primary-replica setups.
Important for the understanding of this connection should be how and when it picks the replica or primary.
- Replica if primary was never picked before and ONLY if 'getWrappedConnection' or 'executeQuery' is used.
- Primary picked when 'executeStatement', 'insert', 'delete', 'update', 'createSavepoint', 'releaseSavepoint', 'beginTransaction', 'rollback', 'commit' or 'prepare' is called.
- If Primary was picked once during the lifetime of the connection it will always get picked afterwards.
- One replica connection is randomly picked ONCE during a request.
ATTENTION: You can write to the replica with this connection if you execute a write query without opening up a transaction. For example:
$conn = DriverManager::getConnection(...);
$conn->executeQuery("DELETE FROM table");
Be aware that Connection#executeQuery is a method specifically for READ operations only.
Use Connection#executeStatement for any SQL statement that changes/updates state in the database (UPDATE, INSERT, DELETE or DDL statements).
This connection is limited to replica operations using the Connection#executeQuery operation only, because it wouldn't be compatible with the ORM or SchemaManager code otherwise. Both use all the other operations in a context where writes could happen to a replica, which makes this restricted approach necessary.
You can manually connect to the primary at any time by calling:
$conn->ensureConnectedToPrimary();
Instantiation through the DriverManager looks like:
Tags
Table of Contents
Properties
- $connections : array<string|int, Connection>|array<string|int, null>
- Primary and Replica connection (one of the randomly picked replicas).
- $keepReplica : bool
- You can keep the replica connection and then switch back to it during the request if you know what you are doing.
Methods
- beginTransaction() : mixed
- {@inheritDoc}
- close() : mixed
- {@inheritDoc}
- commit() : mixed
- {@inheritDoc}
- connect() : bool
- createSavepoint() : mixed
- {@inheritDoc}
- ensureConnectedToPrimary() : bool
- Connects to the primary node of the database cluster.
- ensureConnectedToReplica() : bool
- Connects to a replica node of the database cluster.
- executeStatement() : mixed
- {@inheritDoc}
- isConnectedToPrimary() : bool
- Checks if the connection is currently towards the primary or not.
- prepare() : Statement
- releaseSavepoint() : mixed
- {@inheritDoc}
- rollBack() : mixed
- {@inheritDoc}
- rollbackSavepoint() : mixed
- {@inheritDoc}
- chooseConnectionConfiguration() : mixed
- connectTo() : Connection
- Connects to a specific connection.
- performConnect() : bool
Properties
$connections
Primary and Replica connection (one of the randomly picked replicas).
protected
array<string|int, Connection>|array<string|int, null>
$connections
= ['primary' => null, 'replica' => null]
$keepReplica
You can keep the replica connection and then switch back to it during the request if you know what you are doing.
protected
bool
$keepReplica
= false
Methods
beginTransaction()
{@inheritDoc}
public
beginTransaction() : mixed
close()
{@inheritDoc}
public
close() : mixed
commit()
{@inheritDoc}
public
commit() : mixed
connect()
public
connect([string|null $connectionName = null ]) : bool
Parameters
- $connectionName : string|null = null
Return values
boolcreateSavepoint()
{@inheritDoc}
public
createSavepoint(mixed $savepoint) : mixed
Parameters
- $savepoint : mixed
ensureConnectedToPrimary()
Connects to the primary node of the database cluster.
public
ensureConnectedToPrimary() : bool
All following statements after this will be executed against the primary node.
Return values
boolensureConnectedToReplica()
Connects to a replica node of the database cluster.
public
ensureConnectedToReplica() : bool
All following statements after this will be executed against the replica node, unless the keepReplica option is set to false and a primary connection was already opened.
Return values
boolexecuteStatement()
{@inheritDoc}
public
executeStatement(mixed $sql[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $types = [] ]) : mixed
Parameters
- $sql : mixed
- $params : array<string|int, mixed> = []
- $types : array<string|int, mixed> = []
isConnectedToPrimary()
Checks if the connection is currently towards the primary or not.
public
isConnectedToPrimary() : bool
Return values
boolprepare()
public
prepare(string $sql) : Statement
Parameters
- $sql : string
Return values
StatementreleaseSavepoint()
{@inheritDoc}
public
releaseSavepoint(mixed $savepoint) : mixed
Parameters
- $savepoint : mixed
rollBack()
{@inheritDoc}
public
rollBack() : mixed
rollbackSavepoint()
{@inheritDoc}
public
rollbackSavepoint(mixed $savepoint) : mixed
Parameters
- $savepoint : mixed
chooseConnectionConfiguration()
protected
chooseConnectionConfiguration(string $connectionName, array<string|int, mixed> $params) : mixed
Parameters
- $connectionName : string
- $params : array<string|int, mixed>
connectTo()
Connects to a specific connection.
protected
connectTo(string $connectionName) : Connection
Parameters
- $connectionName : string
Tags
Return values
ConnectionperformConnect()
protected
performConnect([string|null $connectionName = null ]) : bool
Parameters
- $connectionName : string|null = null