jardispsr/dbconnection 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jardispsr/dbconnection

最新稳定版本:1.0.0

Composer 安装命令:

composer require jardispsr/dbconnection

包简介

This package provides dbconnection interfaces for a domain driven design approach

README 文档

README

Build Status License: MIT PHP Version PHPStan Level PSR-4 PSR-12

This package provides interface contracts for database connection management with read/write splitting support. Implementations can be used in any PHP environment, including long-running processes like RoadRunner, Swoole, or FrankenPHP.

Important: This is a contract-only package. It defines interfaces but does not provide implementations.

Features

  • Read/Write Splitting: Separate connections for primary (write) and replica (read) databases
  • Connection Management: Handle connection lifecycle, validation, and reconnection
  • Automatic Load Balancing: Distribute read operations across multiple replicas
  • Failover Support: Automatic failover when replicas become unhealthy
  • Statistics Tracking: Monitor read/write counts and failover events

Requirements

  • PHP >= 8.2
  • ext-pdo

Installation

composer require jardispsr/dbconnection

Interfaces

DbConnectionInterface

Core interface for database connections with support for:

  • PDO instance access (pdo())
  • Connection validation (isConnected())
  • Connection lifecycle management (disconnect(), reconnect())
  • Transaction management (beginTransaction(), commit(), rollback(), inTransaction())
  • Metadata access (getDriverName(), getDatabaseName())
  • Default PDO options constant (DEFAULT_OPTIONS)

ConnectionPoolInterface

Interface for read/write splitting in database replication setups:

  • getWriter(): Returns a DbConnectionInterface for write operations (primary database)
  • getReader(): Returns a DbConnectionInterface for read operations (replica database)
  • getStats(): Returns array with statistics: {reads: int, writes: int, failovers: int, readers: int}
  • resetStats(): Resets all statistics counters to zero

Implementations are expected to provide:

  • Lazy connection creation
  • Load balancing across multiple replicas
  • Automatic failover when replicas become unhealthy
  • Fallback to writer when no readers are configured

Usage Example

use JardisPsr\DbConnection\ConnectionPoolInterface;
use JardisPsr\DbConnection\DbConnectionInterface;

// Assuming you have an implementation of ConnectionPoolInterface
// (this package only provides the interface contracts)

/** @var ConnectionPoolInterface $pool */

// Write operations - use the primary database
$writer = $pool->getWriter();
$pdo = $writer->pdo();

$stmt = $pdo->prepare('INSERT INTO users (name, email) VALUES (?, ?)');
$stmt->execute(['John Doe', 'john@example.com']);

// Read operations - use a replica database
$reader = $pool->getReader();
$pdo = $reader->pdo();

$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([1]);
$user = $stmt->fetch();

// Monitor pool statistics
$stats = $pool->getStats();
echo sprintf(
    "Reads: %d, Writes: %d, Failovers: %d, Readers: %d\n",
    $stats['reads'],
    $stats['writes'],
    $stats['failovers'],
    $stats['readers']
);

// Reset statistics for new monitoring period
$pool->resetStats();

Why Read/Write Splitting?

In database replication setups, the primary database handles writes while replicas handle reads. Read/write splitting:

  1. Scales read operations: Distribute read load across multiple replicas
  2. Reduces primary load: Offload read queries from the primary database
  3. Improves performance: Read queries don't compete with write operations
  4. Provides failover: Automatically handle replica failures
  5. Enables monitoring: Track read/write patterns and failover events

This approach is particularly beneficial in long-running PHP processes (RoadRunner, Swoole, FrankenPHP) where database connections persist across multiple requests.

Development

Code Quality

# Run PHPStan (level 8)
make phpstan

# Run PHP CodeSniffer (PSR-12)
make phpcs

All commands require Docker Compose and a .env file (see Makefile for details).

License

MIT License - see LICENSE file for details.

Support

Authors

  • Jardis Core Development Team

统计信息

  • 总下载量: 85
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-27