jardisadapter/dbconnection
Composer 安装命令:
composer require jardisadapter/dbconnection
包简介
PDO connection pool for PHP with read/write splitting, round-robin load balancing across replicas, and automatic health checks — supports MySQL, PostgreSQL, and SQLite; a building block of the open-source foundation that Jardis-generated DDD code runs on
关键字:
README 文档
README
Part of Jardis — the Domain-Driven Design platform for PHP. You model your domain; Jardis generates the production-ready hexagonal code (DTOs, Command/Query handlers, repositories, persistence). This package is part of the open-source foundation that generated code runs on.
A PDO connection pool with read/write splitting for PHP — round-robin load balancing across replicas, and automatic health checks. Create typed connections for MySQL, PostgreSQL, or SQLite via ConnectionFactory, then compose them into a ConnectionPool for replica-aware query routing. Health checks run SELECT 1 with a configurable TTL cache so failover is fast and non-intrusive.
Features
- Read/Write Splitting — Route writes to the primary and reads to replicas automatically
- Round-Robin Load Balancing — Distributes read queries evenly across all configured readers
- Health Checks —
SELECT 1validation with positive and negative result caching - Transaction Support —
beginTransaction(),commit(),rollback(),inTransaction()on every connection - Reconnect —
reconnect()rebuilds the PDO connection from config on failure - MySQL / PostgreSQL / SQLite — Dedicated factory methods per driver with typed config
- External PDO Wrapping —
fromPdo()integrates existing connections without refactoring - ConnectionPool Stats —
getStats()exposes reads, writes, and failover counts at runtime
Installation
composer require jardisadapter/dbconnection
Quick Start
use JardisAdapter\DbConnection\Factory\ConnectionFactory; $factory = new ConnectionFactory(); // Create a MySQL connection and run a query $connection = $factory->mysql( host: 'localhost', user: 'app_user', password: 'secret', database: 'mydb' ); $pdo = $connection->pdo(); $users = $pdo->query('SELECT * FROM users')->fetchAll();
Advanced Usage
use JardisAdapter\DbConnection\Factory\ConnectionFactory; use JardisAdapter\DbConnection\ConnectionPool; use JardisAdapter\DbConnection\Config\ConnectionPoolConfig; $factory = new ConnectionFactory(); // Primary writer + two read replicas $pool = new ConnectionPool( writer: $factory->mysql('primary.db', 'user', 'secret', 'mydb'), readers: [ $factory->mysql('replica1.db', 'user', 'secret', 'mydb'), $factory->mysql('replica2.db', 'user', 'secret', 'mydb'), ], config: new ConnectionPoolConfig(validateConnections: true) ); // Writes go to the primary $pool->getWriter()->pdo()->exec('INSERT INTO orders (total) VALUES (99.99)'); // Reads are distributed round-robin across replicas $orders = $pool->getReader()->pdo()->query('SELECT * FROM orders')->fetchAll(); // Inspect pool activity $stats = $pool->getStats(); // ['reads' => 1, 'writes' => 1, 'failovers' => 0, 'readers' => 2] // Transactions on a dedicated connection $conn = $factory->postgres('localhost', 'user', 'secret', 'mydb'); $conn->beginTransaction(); try { $conn->pdo()->exec('UPDATE accounts SET balance = balance - 100 WHERE id = 1'); $conn->pdo()->exec('UPDATE accounts SET balance = balance + 100 WHERE id = 2'); $conn->commit(); } catch (\Throwable $e) { $conn->rollback(); throw $e; } // Wrap an existing PDO from a legacy system $legacy = $factory->fromPdo($existingPdo);
PDO Connection Options
All driver factory methods accept an options array passed directly to the PDO constructor. This is useful for long-running processes (RoadRunner, Swoole, FrankenPHP) where persistent connections avoid reconnect overhead per request:
use PDO; $connection = $factory->mysql( host: 'localhost', user: 'app_user', password: 'secret', database: 'mydb', options: [ PDO::ATTR_PERSISTENT => true, PDO::ATTR_TIMEOUT => 5, ] );
Note: Persistent connections are reused across requests within the same worker process. Ensure your database server is configured for the expected number of concurrent connections (workers × connections per worker).
These options apply per connection — in a ConnectionPool, each connection can have its own settings.
Documentation
Full documentation, guides, and API reference:
docs.jardis.io/en/adapter/dbconnection
License
This package is licensed under the MIT License.
Jardis · Documentation · Headgent
AI-Assisted Development
This package ships with a skill for Claude Code, Cursor, Continue, and Aider. Install it in your consuming project:
composer require --dev jardis/dev-skills
More details: https://docs.jardis.io/en/skills
jardisadapter/dbconnection 适用场景与选型建议
jardisadapter/dbconnection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 337 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「mysql」 「sqlite」 「postgresql」 「pdo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jardisadapter/dbconnection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jardisadapter/dbconnection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jardisadapter/dbconnection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
PHP module for MySql database
VV database abstraction layer with query builder and DB structure models
统计信息
- 总下载量: 337
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 42
- 依赖项目数: 2
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-09