Chunk #551

Aus: Infrastructure (Index: 0)

296 Tokens
Synced Status
Nächster (#1) »

Taxonomie

Kategorie Infrastructure
Pfad Infrastructure > DatabaseConnection > Connection Pool
Heading-Pfad Infrastructure > MCP-DB Infrastructure > DatabaseConnection

Entities

Name Typ
MySQL Connection Pool TECHNOLOGY
Config CONFIG
Database TECHNOLOGY
Singleton CONCEPT
contextmanager SERVICE

Keywords

Connection Pool MySQL Database Singleton Database Connection

Inhalt

Singleton Connection Pool für Datenbankzugriffe.

"""Database Connection Pool"""
import os
from contextlib import contextmanager
import mysql.connector
from mysql.connector import pooling
from config import Config

class DatabaseConnection:
    """Singleton Connection Pool - SRP: Nur Verbindungsmanagement"""

    _pool = None

    @classmethod
    def get_pool(cls):
        if cls._pool is None:
            cls._pool = pooling.MySQLConnectionPool(
                pool_name="mcp_pool",
                pool_size=5,
                host=Config.DB_HOST,
                user=Config.DB_USER,
                password=Config.DB_PASSWORD,
                charset="utf8mb4",
                connection_timeout=10,
                autocommit=True
            )
        return cls._pool

    @classmethod
    @contextmanager
    def get_connection(cls, database: str):
        """Context Manager für DB Connection."""
        conn = cls.get_pool().get_connection()
        try:
            # Datenbank wechseln (USE statement statt property)
            cursor = conn.cursor()
            cursor.execute(f"USE {database}")

            # Query Timeout setzen (MariaDB verwendet max_statement_time in Sekunden)
            try:
                cursor.execute(
                    f"SET SESSION max_statement_time = {Config.QUERY_TIMEOUT_SEC}"
                )
            except Exception:
                pass  # Falls Variable nicht unterstützt wird
            cursor.close()

            yield conn
        finally:
            conn.close()

Clean Content

Singleton Connection Pool für Datenbankzugriffe.

```
`"""Database Connection Pool"""
import os
from contextlib import contextmanager
import mysql.connector
from mysql.connector import pooling
from config import Config

class DatabaseConnection:
 """Singleton Connection Pool - SRP: Nur Verbindungsmanagement"""

 _pool = None

 @classmethod
 def get_pool(cls):
 if cls._pool is None:
 cls._pool = pooling.MySQLConnectionPool(
 pool_name="mcp_pool",
 pool_size=5,
 host=Config.DB_HOST,
 user=Config.DB_USER,
 password=Config.DB_PASSWORD,
 charset="utf8mb4",
 connection_timeout=10,
 autocommit=True
 )
 return cls._pool

 @classmethod
 @contextmanager
 def get_connection(cls, database: str):
 """Context Manager für DB Connection."""
 conn = cls.get_pool().get_connection()
 try:
 # Datenbank wechseln (USE statement statt property)
 cursor = conn.cursor()
 cursor.execute(f"USE {database}")

 # Query Timeout setzen (MariaDB verwendet max_statement_time in Sekunden)
 try:
 cursor.execute(
 f"SET SESSION max_statement_time = {Config.QUERY_TIMEOUT_SEC}"
 )
 except Exception:
 pass # Falls Variable nicht unterstützt wird
 cursor.close()

 yield conn
 finally:
 conn.close()`
```

Metadaten

Analysiert am20.12.2025 12:44:03
Analyse-Modellgemma3:4b-it-qat
Embedding-Modellmxbai-embed-large
Qdrant-ID5668b9f9-d1bd-4d1a-88cd-8aa63793ac59
Erstellt20.12.2025 12:34:42
Aktualisiert20.12.2025 12:53:39