PHP code example of utopia-php / pools

1. Go to this page and download the library: Download utopia-php/pools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

utopia-php / pools example snippets


use PDO;
use Utopia\Pools\Pool;
use Utopia\Pools\Group;

$pool = new Pool('mysql-pool', 1 /* number of connections */, function() {
    $host = '127.0.0.1';
    $db   = 'test';
    $user = 'root';
    $pass = '';
    $charset = 'utf8mb4';

    try {
        $pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);
    } catch (\PDOException $e) {
        throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }

    return $pdo;
});

$pool->setReconnectAttempts(3); // number of attempts to reconnect
$pool->setReconnectSleep(5); // seconds to sleep between reconnect attempts

$pool->setRetryAttempts(3); // number of attempts to get connection
$pool->setRetrySleep(5); // seconds to sleep between failed pop-connection attempts

$connection = $pool->pop(); // Get a connection from the pool
$connection->getID(); // Get the connection ID
$connection->getResource(); // Get the connection resource

$pool->push($connection); // Return the connection to the pool

$pool->reclaim(); // Recalim the pool, return all active connections automatically

$pool->count(); // Get the number of available connections

$pool->isEmpty(); // Check if the pool is empty

$pool->isFull(); // Check if the pool is full

$group = new Group(); // Create a group of pools
$group->add($pool); // Add a pool to the group
$group->get('mysql-pool'); // Get a pool from the group
$group->setReconnectAttempts(3); // Set the number of reconnect attempts for all pools
$group->setReconnectSleep(5); // Set the sleep time between reconnect attempts for all pools
bash
composer