1. Go to this page and download the library: Download senza1dio/database-pool 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/ */
senza1dio / database-pool example snippets
Senza1dio\DatabasePool\Config\PoolConfig;
use Senza1dio\DatabasePool\DatabasePool;
// Configure pool once at application boot
$config = (new PoolConfig())
->setHost('localhost')
->setPort(5432)
->setDatabase('myapp')
->setCredentials('postgres', 'secret')
->setPoolSize(5, 50);
$pool = new DatabasePool($config);
// Handle requests (pool persists across requests)
while ($request = $server->acceptRequest()) {
$pdo = $pool->getConnection();
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([123]);
$user = $stmt->fetch();
// Connection automatically released and returned to pool
}
Senza1dio\DatabasePool\Config\PoolConfig;
use Senza1dio\DatabasePool\DatabasePool;
$config = (new PoolConfig())
->setHost('localhost')
->setPort(5432)
->setDatabase('myapp')
->setCredentials('postgres', 'secret')
->setPoolSize(5, 50);
$pool = new DatabasePool($config);
// Process queue messages (long-running daemon)
while (true) {
$message = $queue->pop();
$pdo = $pool->getConnection();
$pdo->exec("INSERT INTO processed VALUES (...)");
// Connection auto-released, reused on next iteration
}
// config/database-pool.php
return [
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 5432),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', ''),
'minConnections' => 10,
'maxConnections' => 100,
];
// app/Providers/DatabasePoolServiceProvider.php
use Senza1dio\DatabasePool\Config\PoolConfig;
use Senza1dio\DatabasePool\DatabasePool;
use Senza1dio\DatabasePool\Adapters\Locks\RedisLock;
use Senza1dio\DatabasePool\Adapters\Loggers\Psr3LoggerAdapter;
$this->app->singleton(DatabasePool::class, function ($app) {
$config = PoolConfig::fromArray(config('database-pool'))
->setLock(new RedisLock(Redis::connection()->client()))
->setLogger(new Psr3LoggerAdapter(Log::channel('database')));
return new DatabasePool($config);
});
// Usage in controllers
public function __construct(DatabasePool $pool) {
$this->pool = $pool;
}
public function show($id) {
$pdo = $this->pool->getConnection();
// ... use PDO
}
use Senza1dio\DatabasePool\Config\PoolConfig;
use Senza1dio\DatabasePool\DatabasePoolSingleton;
$config = (new PoolConfig())
->setHost('localhost')
->setPort(5432)
->setDatabase('myapp')
->setCredentials('postgres', 'secret')
->setPoolSize(5, 50)
->enablePersistentConnections(true);
// Get singleton instance (persists across requests in same worker)
$pool = DatabasePoolSingleton::getInstance($config);
$pdo = $pool->getConnection();
// ❌ WRONG - Creates new config object each time (will throw LogicException)
$pool = DatabasePoolSingleton::getInstance(PoolConfig::fromArray($config));
// ✅ CORRECT - Reuse same config instance
static $poolConfig = null;
if ($poolConfig === null) {
$poolConfig = PoolConfig::fromArray($config);
}
$pool = DatabasePoolSingleton::getInstance($poolConfig);
$config = (new PoolConfig())
// Database connection
->setHost('localhost')
->setPort(5432)
->setDatabase('myapp')
->setCredentials('user', 'pass')
->setCharset('utf8')
// Pool sizing
->setPoolSize(10, 100) // Min 10, Max 100
// SSL/TLS (recommended for production)
->enableSsl(
verify: true,
// Performance tuning
->setSlowQueryThreshold(2.0) // Log queries >2s
->setIdleTimeout(1500) // Close idle connections after 25min
->enableAutoScaling(true) // Auto-scale based on load
// Dependencies (optional)
->setLogger($monolog)
->setLock($redisLock)
// Metadata (for monitoring)
->setApplicationName('myapp')
->setTimezone('Europe/Rome');
$pdo = $pool->getConnection();
$pdo->beginTransaction();
$pdo->exec('UPDATE accounts SET balance = balance - 100 WHERE id = 1');
// Developer forgets to commit/rollback
unset($pdo); // Connection released
// AUTOMATIC ROLLBACK HAPPENS HERE
// No data corruption! Transaction rolled back safely.
$metrics = $pool->getMetrics();
echo "Auto-rollbacks: {$metrics['transaction_rollbacks']}";
$config = (new PoolConfig())
->enableSsl(
verify: true, // Verify server certificate
/client.key'
);
$config = (new PoolConfig())
->setQueryLimits(
maxSize: 1048576, // 1MB max query (prevents memory exhaustion)
maxParams: 1000 // Max 1000 params (prevents parameter bombing)
);
try {
$pdo = $pool->getConnection();
} catch (CircuitBreakerOpenException $e) {
// Circuit breaker is open
// Retry after $e->getTimeout() seconds
sleep(20);
$pdo = $pool->getConnection();
}
use Senza1dio\DatabasePool\Adapters\Locks\RedisLock;
$redis = new Redis();
$redis->connect('localhost', 6379);
$config = (new PoolConfig())
// ... other config
->setLock(new RedisLock($redis, 'myapp:pool:'));
$pool = new DatabasePool($config);
use Senza1dio\DatabasePool\Adapters\Loggers\Psr3LoggerAdapter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logger = new Logger('database');
$logger->pushHandler(new StreamHandler('php://stdout'));
$config = (new PoolConfig())
// ... other config
->setLogger(new Psr3LoggerAdapter($logger));
$pool = new DatabasePool($config);
// Slow queries, errors, circuit breaker events logged automatically