style: normalize python formatting gates
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"""
|
||||
数据库连接池管理
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import psycopg2
|
||||
from psycopg2 import pool
|
||||
from psycopg2.extras import RealDictCursor
|
||||
@@ -9,15 +11,15 @@ from psycopg2.extras import RealDictCursor
|
||||
|
||||
class DatabaseConnectionPool:
|
||||
"""PostgreSQL 连接池"""
|
||||
|
||||
_instance: Optional['DatabaseConnectionPool'] = None
|
||||
|
||||
_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,
|
||||
@@ -31,18 +33,18 @@ class DatabaseConnectionPool:
|
||||
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:
|
||||
@@ -56,17 +58,17 @@ 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:
|
||||
|
||||
Reference in New Issue
Block a user