Doctrine\DBAL\Connections\MasterSlaveConnection
Master-Slave Connection
Connection can be used with master-slave setups.
Important for the understanding of this connection should be how and when
it picks the slave or master.
1. Slave if master was never picked before and ONLY if 'getWrappedConnection'
or 'executeQuery' is used.
2. Master picked when 'exec', 'executeUpdate', 'insert', 'delete', 'update', 'createSavepoint',
'releaseSavepoint', 'beginTransaction', 'rollback', 'commit', 'query' or
'prepare' is called.
3. If master was picked once during the lifetime of the connection it will always get picked afterwards.
4. One slave connection is randomly picked ONCE during a request.
ATTENTION: You can write to the slave 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.
This connection is limited to slave 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 slave, which makes
this restricted approach necessary.
You can manually connect to the master at any time by calling:
$conn->connect('master');
Instantiation through the DriverManager looks like:
- Author: Lars Strojny <lstrojny@php.net>
Synopsis
- // Inherited constants from Connection
- const TRANSACTION_READ_UNCOMMITTED = 1;
- const TRANSACTION_READ_COMMITTED = 2;
- const TRANSACTION_REPEATABLE_READ = 3;
- const TRANSACTION_SERIALIZABLE = 4;
- const PARAM_INT_ARRAY = 101;
- const PARAM_STR_ARRAY = 102;
- const ARRAY_PARAM_OFFSET = 100;
- // members
- protected array $connections;
- protected bool $keepSlave;
- // Inherited members from Connection
- protected Connection $_conn;
- protected Configuration $_config;
- protected EventManager $_eventManager;
- protected ExpressionBuilder $_expr;
- protected AbstractPlatform $_platform;
- protected AbstractSchemaManager $_schemaManager;
- protected Driver $_driver;
- // methods
- public void __construct()
- public bool isConnectedToMaster()
- public void connect()
- protected Driver connectTo()
- protected void chooseConnectionConfiguration()
- public void executeUpdate()
- public void beginTransaction()
- public void commit()
- public void rollBack()
- public void delete()
- public void update()
- public void insert()
- public void exec()
- public void createSavepoint()
- public void releaseSavepoint()
- public void rollbackSavepoint()
- public void query()
- public void prepare()
- // Inherited methods from Connection
- public void __construct()
- public array getParams()
- public string getDatabase()
- public string getHost()
- public mixed getPort()
- public string getUsername()
- public string getPassword()
- public Driver getDriver()
- public Configuration getConfiguration()
- public EventManager getEventManager()
- public AbstractPlatform getDatabasePlatform()
- public ExpressionBuilder getExpressionBuilder()
- public boolean connect()
- public void setFetchMode()
- public array fetchAssoc()
- public array fetchArray()
- public mixed fetchColumn()
- public boolean isConnected()
- public boolean isTransactionActive()
- public integer delete()
- public void close()
- public integer setTransactionIsolation()
- public integer getTransactionIsolation()
- public integer update()
- public integer insert()
- public string quoteIdentifier()
- public string quote()
- public array fetchAll()
- public Statement prepare()
- public Statement executeQuery()
- public ResultStatement executeCacheQuery()
- public mixed project()
- public Statement query()
- public integer executeUpdate()
- public integer exec()
- public integer getTransactionNestingLevel()
- public integer errorCode()
- public array errorInfo()
- public string lastInsertId()
- public void transactional()
- public void setNestTransactionsWithSavepoints()
- public boolean getNestTransactionsWithSavepoints()
- protected mixed _getNestedTransactionSavePointName()
- public void beginTransaction()
- public void commit()
- public void rollBack()
- public void createSavepoint()
- public void releaseSavepoint()
- public void rollbackSavepoint()
- public Connection getWrappedConnection()
- public AbstractSchemaManager getSchemaManager()
- public void setRollbackOnly()
- public boolean isRollbackOnly()
- public mixed convertToDatabaseValue()
- public mixed convertToPHPValue()
- public array resolveParams()
- public QueryBuilder createQueryBuilder()
Hierarchy
Extends
Constants
Name | Value |
---|---|
TRANSACTION_READ_UNCOMMITTED | 1 |
TRANSACTION_READ_COMMITTED | 2 |
TRANSACTION_REPEATABLE_READ | 3 |
TRANSACTION_SERIALIZABLE | 4 |
PARAM_INT_ARRAY | 101 |
PARAM_STR_ARRAY | 102 |
ARRAY_PARAM_OFFSET | 100 |
Members
protected
- $_config — \Doctrine\DBAL\Configuration
- $_conn
—
\Doctrine\DBAL\Driver\Connection
The wrapped driver connection. - $_driver
—
\Doctrine\DBAL\Driver
The used DBAL driver. - $_eventManager — \Doctrine\Common\EventManager
- $_expr — \Doctrine\DBAL\Query\Expression\ExpressionBuilder
- $_platform
—
\Doctrine\DBAL\Platforms\AbstractPlatform
The DatabasePlatform object that provides information about the database platform used by the connection. - $_schemaManager
—
\Doctrine\DBAL\Schema\AbstractSchemaManager
The schema manager. - $connections
—
array
Master and slave connection (one of the randomly picked slaves) - $keepSlave
—
Doctrine\DBAL\Connections\bool
You can keep the slave connection and then switch back to it during the request if you know what you are doing.
Methods
protected
- chooseConnectionConfiguration()
- connectTo() — Connect to a specific connection
public
- __construct() — Create Master Slave Connection
- beginTransaction() — {@inheritDoc}
- commit() — {@inheritDoc}
- connect() — {@inheritDoc}
- createSavepoint() — {@inheritDoc}
- delete() — {@inheritDoc}
- exec() — {@inheritDoc}
- executeUpdate() — {@inheritDoc}
- insert() — {@inheritDoc}
- isConnectedToMaster() — Check if the connection is currently towards the master or not.
- prepare()
- query()
- releaseSavepoint() — {@inheritDoc}
- rollBack() — {@inheritDoc}
- rollbackSavepoint() — {@inheritDoc}
- update() — {@inheritDoc}
Inherited from Doctrine\DBAL\Connection
protected
- _getNestedTransactionSavePointName() — Returns the savepoint name to use for nested transactions are false if they are not supported "savepointFormat" parameter is not set
public
- beginTransaction() — Starts a transaction by suspending auto-commit mode.
- close() — Closes the connection.
- commit() — Commits the current transaction.
- connect() — Establishes the connection with the database.
- convertToDatabaseValue() — Converts a given value to its database representation according to the conversion rules of a specific DBAL mapping type.
- convertToPHPValue() — Converts a given value to its PHP representation according to the conversion rules of a specific DBAL mapping type.
- createQueryBuilder() — Create a new instance of a SQL query builder.
- createSavepoint() — createSavepoint creates a new savepoint
- delete() — Executes an SQL DELETE statement on a table.
- errorCode() — Fetch the SQLSTATE associated with the last database operation.
- errorInfo() — Fetch extended error information associated with the last database operation.
- exec() — Execute an SQL statement and return the number of affected rows.
- executeCacheQuery() — Execute a caching query and
- executeQuery() — Executes an, optionally parameterized, SQL query.
- executeUpdate() — Executes an SQL INSERT/UPDATE/DELETE query with the given parameters and returns the number of affected rows.
- fetchAll() — Prepares and executes an SQL query and returns the result as an associative array.
- fetchArray() — Prepares and executes an SQL query and returns the first row of the result as a numerically indexed array.
- fetchAssoc() — Prepares and executes an SQL query and returns the first row of the result as an associative array.
- fetchColumn() — Prepares and executes an SQL query and returns the value of a single column of the first row of the result.
- getConfiguration() — Gets the Configuration used by the Connection.
- getDatabase() — Gets the name of the database this Connection is connected to.
- getDatabasePlatform() — Gets the DatabasePlatform for the connection.
- getDriver() — Gets the DBAL driver instance.
- getEventManager() — Gets the EventManager used by the Connection.
- getExpressionBuilder() — Gets the ExpressionBuilder for the connection.
- getHost() — Gets the hostname of the currently connected database.
- getNestTransactionsWithSavepoints() — Get if nested transactions should use savepoints
- getParams() — Gets the parameters used during instantiation.
- getPassword() — Gets the password used by this connection.
- getPort() — Gets the port of the currently connected database.
- getSchemaManager() — Gets the SchemaManager that can be used to inspect or change the database schema through the connection.
- getTransactionIsolation() — Gets the currently active transaction isolation level.
- getTransactionNestingLevel() — Returns the current transaction nesting level.
- getUsername() — Gets the username used by this connection.
- getWrappedConnection() — Gets the wrapped driver connection.
- insert() — Inserts a table row with specified data.
- isConnected() — Whether an actual connection to the database is established.
- isRollbackOnly() — Check whether the current transaction is marked for rollback only.
- isTransactionActive() — Checks whether a transaction is currently active.
- lastInsertId() — Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver.
- prepare() — Prepares an SQL statement.
- project() — Executes an, optionally parameterized, SQL query and returns the result, applying a given projection/transformation function on each row of the result.
- query() — Executes an SQL statement, returning a result set as a Statement object.
- quote() — Quotes a given input parameter.
- quoteIdentifier() — Quote a string so it can be safely used as a table or column name, even if it is a reserved name.
- releaseSavepoint() — releaseSavePoint releases given savepoint
- resolveParams() — Resolves the parameters to a format which can be displayed.
- rollBack() — Cancel any database changes done during the current transaction.
- rollbackSavepoint() — rollbackSavePoint releases given savepoint
- setFetchMode() — setFetchMode
- setNestTransactionsWithSavepoints() — Set if nested transactions should use savepoints
- setRollbackOnly() — Marks the current transaction so that the only possible outcome for the transaction to be rolled back.
- setTransactionIsolation() — Sets the transaction isolation level.
- transactional() — Executes a function in a transaction.
- update() — Executes an SQL UPDATE statement on a table.