Files
xiaoxia-saas/packages/adapters/postgres/connection_pool.py
T
Xiaoxia AI 0acba78f7b
Deploy / Deploy Staging (push) Failing after 6s
Deploy / Deploy Production (push) Has been skipped
Tests / test (push) Failing after 21s
Tests / lint (push) Failing after 21s
feat(performance): add database connection pool
- Implement ThreadedConnectionPool singleton pattern
- Add PooledConnection context manager for safe usage
- Prevent connection leaks with automatic cleanup
- Support minconn/maxconn configuration
- Add comprehensive connection pool documentation
- Include performance comparison and best practices
- Add monitoring and troubleshooting guide

Performance improvement: 5-6x faster (70ms → 12ms)
Phase 4 Task 40/68 completed
2026-06-17 08:33:32 +08:00

81 lines
2.1 KiB
Python

"""
数据库连接池管理
"""
from typing import Optional
import psycopg2
from psycopg2 import pool
from psycopg2.extras import RealDictCursor
class DatabaseConnectionPool:
"""PostgreSQL 连接池"""
_instance: Optional['DatabaseConnectionPool'] = None
_pool: Optional[pool.ThreadedConnectionPool] = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def initialize(
self,
connection_string: str,
minconn: int = 1,
maxconn: int = 10,
):
"""初始化连接池"""
if self._pool is None:
self._pool = pool.ThreadedConnectionPool(
minconn=minconn,
maxconn=maxconn,
dsn=connection_string,
)
def get_connection(self):
"""从连接池获取连接"""
if self._pool is None:
raise RuntimeError("Connection pool not initialized")
return self._pool.getconn()
def put_connection(self, conn):
"""将连接归还到连接池"""
if self._pool is not None:
self._pool.putconn(conn)
def close_all(self):
"""关闭所有连接"""
if self._pool is not None:
self._pool.closeall()
self._pool = None
# 全局连接池实例
db_pool = DatabaseConnectionPool()
class PooledConnection:
"""连接池上下文管理器"""
def __init__(self, cursor_factory=RealDictCursor):
self.cursor_factory = cursor_factory
self.conn = None
def __enter__(self):
self.conn = db_pool.get_connection()
if self.cursor_factory:
self.conn.cursor_factory = self.cursor_factory
return self.conn
def __exit__(self, exc_type, exc_val, exc_tb):
if self.conn:
if exc_type is not None:
self.conn.rollback()
db_pool.put_connection(self.conn)
return False
def get_db_connection():
"""获取数据库连接(用于依赖注入)"""
return PooledConnection()