Simple connection pool for mysql client using connection
Table of contents
$pool = new \Nstwf\MysqlConnectionPool\Pool('localhost:3306');
$pool
->getConnection()
->then(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) use ($pool) {
$connection->query("UPDATE users SET blocked = 1 WHERE id = 3");
$pool->releaseConnection($connection);
});
The main role of PoolInterface
- managing connections with selected options
waitForConnections: bool
, set up the behavior while no free connections exists and user callgetConnection
method. If set tofalse
- throws an exception, otherwise return a promise with free connection. (Default:true
)connectionLimit: int
, the maximum number of connections at the same time.0
- for unlimited. (Default:5
)
$pool = new \Nstwf\MysqlConnectionPool\Pool('localhost:3306', null, 10, false);
The getConnection(): PromiseInterface<ConnectionInterface>
method can be used to create a new ConnectionInterface
instance if no free connections available, otherwise select one of free
$pool
->getConnection()
->then(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) {
$connection->query("UPDATE users SET active = 0 WHERE id = 2");
$connection->query("UPDATE users SET blocked = 1 WHERE id = 3");
$pool->releaseConnection($connection);
});
The releaseConnection(ConnectionInterface $connection): void
method can be used to release connection to the pool
$pool->releaseConnection($connection);
The query(string $sql, array $params = []): PromiseInterface<QueryResult>
method is a shortcut for calls getConnection()
-> query()
-> releaseConnection()
$pool->query("UPDATE users SET active = 0 WHERE id = ?", [2]);
The transaction(callable $callable): PromiseInterface
method is a shortcut for calls: getConnection()
-> transaction()
-> releaseConnection()
:
$pool->transaction(function(\Nstwf\MysqlConnection\ConnectionInterface $connection) {
$connection->query("UPDATE users SET active = 0 WHERE id = 2");
});
The recommended way to install this library is through Composer. New to Composer?
This project follows SemVer. This will install the latest supported version:
composer require nstwf/mysql-connection
See also the CHANGELOG for details about version upgrades.
It's highly recommended to use PHP 8+ * for this project.
To run the test suite, you first need to clone this repo and then install all dependencies through Composer:
composer install
To run the test suite, go to the project root and run:
vendor/bin/phpunit
MIT, see LICENSE file.
- friends-of-reactphp/mysql - main project
- mysqljs/mysql - main concept