Skip to content

Commit 48da618

Browse files
committed
⚡ perf(mcp): 隔离同步数据库调用与事件循环
1 parent bfbf3af commit 48da618

6 files changed

Lines changed: 464 additions & 133 deletions

File tree

‎src/dbjavagenix/database/atomic_codegen_tools.py‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424

2525
import json
26+
import asyncio
2627
import logging
2728
from typing import Any, Dict, List
2829

@@ -34,6 +35,20 @@
3435
logger = logging.getLogger(__name__)
3536

3637

38+
async def _run_db_call(callable_obj, *args):
39+
"""Run blocking database work outside the MCP event loop."""
40+
return await asyncio.to_thread(callable_obj, *args)
41+
42+
43+
async def _run_async_db_call(callable_obj, *args, **kwargs):
44+
"""Run the async analyzer in a worker because its introspection is synchronous."""
45+
46+
def run():
47+
return asyncio.run(callable_obj(*args, **kwargs))
48+
49+
return await asyncio.to_thread(run)
50+
51+
3752
# ============================================================
3853
# Tool 定义 - 7 个原子工具
3954
# ============================================================
@@ -208,10 +223,11 @@ async def handle_codegen_build_context(arguments: Dict[str, Any]) -> List[TextCo
208223
database = config.database or "information_schema"
209224

210225
# 收集所有表名用于前缀分析(沿用旧逻辑)
211-
all_table_names = _collect_all_table_names(connection_id, config)
226+
all_table_names = await _run_db_call(_collect_all_table_names, connection_id, config)
212227

213228
analyzer = CodegenAnalyzer(connection_manager)
214-
analysis = await analyzer.analyze_table_for_codegen(
229+
analysis = await _run_async_db_call(
230+
analyzer.analyze_table_for_codegen,
215231
connection_id,
216232
table_name,
217233
all_table_names=all_table_names,
@@ -507,9 +523,8 @@ def _compute_file_path(
507523
def _collect_all_table_names(connection_id: str, config) -> List[str]:
508524
"""收集库内所有表名(用于前缀分析)。失败时返回空列表。"""
509525
try:
510-
conn = connection_manager.get_connection(connection_id)
511-
cursor = conn.cursor()
512-
try:
526+
527+
def collect(cursor) -> List[str]:
513528
if config.type.name == "MYSQL":
514529
cursor.execute("SHOW TABLES")
515530
return [row[0] for row in cursor.fetchall()]
@@ -528,6 +543,16 @@ def _collect_all_table_names(connection_id: str, config) -> List[str]:
528543
)
529544
return [row[0] for row in cursor.fetchall()]
530545
return []
546+
547+
if hasattr(connection_manager, "get_cursor"):
548+
with connection_manager.get_cursor(connection_id) as cursor:
549+
return collect(cursor)
550+
551+
# Preserve the small test-double contract used by older integrations.
552+
connection = connection_manager.get_connection(connection_id)
553+
cursor = connection.cursor()
554+
try:
555+
return collect(cursor)
531556
finally:
532557
cursor.close()
533558
except Exception as e: # noqa: BLE001

‎src/dbjavagenix/database/connection_manager.py‎

Lines changed: 107 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Database connection manager for DBJavaGenix MCP tools
33
"""
44
import uuid
5+
import threading
56
from typing import Dict, List, Any, Optional
67
import pymysql
78
import sqlite3
@@ -22,6 +23,10 @@ class ConnectionManager:
2223
def __init__(self):
2324
self.connections: Dict[str, Any] = {}
2425
self.connection_configs: Dict[str, DatabaseConfig] = {}
26+
# Registry access is short-lived; each live connection gets its own
27+
# re-entrant lock so unrelated connections can execute concurrently.
28+
self._registry_lock = threading.RLock()
29+
self._connection_locks: Dict[str, Any] = {}
2530

2631
def create_connection(self, config: DatabaseConfig) -> str:
2732
"""
@@ -74,16 +79,21 @@ def create_connection(self, config: DatabaseConfig) -> str:
7479
)
7580
connection.autocommit = True
7681
elif config.type == DatabaseType.SQLITE:
77-
connection = sqlite3.connect(config.database)
82+
# MCP database work runs in worker threads. SQLite's default
83+
# thread affinity would reject a connection created elsewhere.
84+
connection = sqlite3.connect(config.database, check_same_thread=False)
7885
connection.row_factory = sqlite3.Row # Enable dict-like access
7986
else:
8087
raise DatabaseConnectionError(f"Unsupported database type: {config.type}")
8188

82-
self.connections[connection_id] = connection
83-
# Store config without sensitive data for reference
84-
safe_config = config.model_copy()
85-
safe_config.password = "***" # Mask password
86-
self.connection_configs[connection_id] = safe_config
89+
# Store the connection and its lock atomically with the registry.
90+
with self._registry_lock:
91+
self.connections[connection_id] = connection
92+
self._connection_locks[connection_id] = threading.RLock()
93+
# Store config without sensitive data for reference
94+
safe_config = config.model_copy()
95+
safe_config.password = "***" # Mask password
96+
self.connection_configs[connection_id] = safe_config
8797

8898
logger.info(f"Created connection {connection_id} to {config.type}://{config.host}:{config.port}")
8999
return connection_id
@@ -106,23 +116,53 @@ def get_connection(self, connection_id: str) -> Any:
106116
Raises:
107117
DatabaseConnectionError: If connection not found
108118
"""
109-
if connection_id not in self.connections:
119+
with self._connection_lock(connection_id):
120+
connection = self._get_connection_unlocked(connection_id)
121+
try:
122+
if getattr(connection, "closed", 0):
123+
raise DatabaseConnectionError("connection is closed")
124+
if hasattr(connection, "ping"):
125+
connection.ping(reconnect=True)
126+
except Exception as exc:
127+
logger.warning("Connection %s is dead, removing: %s", connection_id, exc)
128+
self._close_connection_unlocked(connection_id, connection)
129+
raise DatabaseConnectionError(
130+
f"Connection {connection_id} is no longer valid"
131+
) from exc
132+
return connection
133+
134+
@contextmanager
135+
def _connection_lock(self, connection_id: str):
136+
"""Serialize operations for one connection without blocking the registry."""
137+
with self._registry_lock:
138+
lock = self._connection_locks.get(connection_id)
139+
if lock is None or connection_id not in self.connections:
140+
raise DatabaseConnectionError(f"Connection {connection_id} not found")
141+
with lock:
142+
yield
143+
144+
def _get_connection_unlocked(self, connection_id: str) -> Any:
145+
with self._registry_lock:
146+
connection = self.connections.get(connection_id)
147+
if connection is None:
110148
raise DatabaseConnectionError(f"Connection {connection_id} not found")
111-
112-
connection = self.connections[connection_id]
113-
114-
# Test connection is still alive
115-
try:
116-
if getattr(connection, "closed", 0):
117-
raise DatabaseConnectionError("connection is closed")
118-
if hasattr(connection, 'ping'):
119-
connection.ping(reconnect=True)
120-
except Exception as e:
121-
logger.warning(f"Connection {connection_id} is dead, removing: {e}")
122-
self.close_connection(connection_id)
123-
raise DatabaseConnectionError(f"Connection {connection_id} is no longer valid")
124-
125149
return connection
150+
151+
def _close_connection_unlocked(self, connection_id: str, connection: Any) -> bool:
152+
"""Close and remove a connection while its per-connection lock is held."""
153+
try:
154+
connection.close()
155+
closed = True
156+
except Exception as exc:
157+
logger.error("Error closing connection %s: %s", connection_id, exc)
158+
closed = True
159+
finally:
160+
with self._registry_lock:
161+
self.connections.pop(connection_id, None)
162+
self.connection_configs.pop(connection_id, None)
163+
self._connection_locks.pop(connection_id, None)
164+
logger.info("Closed connection %s", connection_id)
165+
return closed
126166

127167
def close_connection(self, connection_id: str) -> bool:
128168
"""
@@ -134,22 +174,19 @@ def close_connection(self, connection_id: str) -> bool:
134174
Returns:
135175
True if connection was closed, False if not found
136176
"""
137-
if connection_id not in self.connections:
177+
with self._registry_lock:
178+
connection = self.connections.get(connection_id)
179+
lock = self._connection_locks.get(connection_id)
180+
if connection is None or lock is None:
138181
return False
139-
140-
try:
141-
connection = self.connections[connection_id]
142-
connection.close()
143-
self.connections.pop(connection_id, None)
144-
self.connection_configs.pop(connection_id, None)
145-
logger.info(f"Closed connection {connection_id}")
146-
return True
147-
except Exception as e:
148-
logger.error(f"Error closing connection {connection_id}: {e}")
149-
# Remove from dict anyway
150-
self.connections.pop(connection_id, None)
151-
self.connection_configs.pop(connection_id, None)
152-
return True
182+
with lock:
183+
# A preceding close may have removed the connection while this
184+
# caller was waiting for the per-connection lock.
185+
with self._registry_lock:
186+
current = self.connections.get(connection_id)
187+
if current is None:
188+
return False
189+
return self._close_connection_unlocked(connection_id, current)
153190

154191
def get_connection_info(self, connection_id: str) -> Optional[DatabaseConfig]:
155192
"""
@@ -161,7 +198,8 @@ def get_connection_info(self, connection_id: str) -> Optional[DatabaseConfig]:
161198
Returns:
162199
Database configuration or None if not found
163200
"""
164-
config = self.connection_configs.get(connection_id)
201+
with self._registry_lock:
202+
config = self.connection_configs.get(connection_id)
165203
return config.model_copy(deep=True) if config else None
166204

167205
def list_connections(self) -> Dict[str, Dict[str, Any]]:
@@ -171,15 +209,18 @@ def list_connections(self) -> Dict[str, Dict[str, Any]]:
171209
Returns:
172210
Dict of connection_id -> connection_info
173211
"""
212+
with self._registry_lock:
213+
configs = list(self.connection_configs.items())
214+
active_ids = set(self.connections)
174215
result = {}
175-
for conn_id, config in self.connection_configs.items():
216+
for conn_id, config in configs:
176217
result[conn_id] = {
177218
"type": config.type,
178219
"host": config.host,
179220
"port": config.port,
180221
"database": config.database,
181222
"username": config.username,
182-
"status": "active" if conn_id in self.connections else "closed"
223+
"status": "active" if conn_id in active_ids else "closed"
183224
}
184225
return result
185226

@@ -194,12 +235,33 @@ def get_cursor(self, connection_id: str):
194235
Yields:
195236
Database cursor
196237
"""
197-
connection = self.get_connection(connection_id)
198-
cursor = connection.cursor()
199-
try:
200-
yield cursor
201-
finally:
202-
cursor.close()
238+
with self._connection_lock(connection_id):
239+
connection = self._get_connection_unlocked(connection_id)
240+
try:
241+
if getattr(connection, "closed", 0):
242+
raise DatabaseConnectionError("connection is closed")
243+
if hasattr(connection, "ping"):
244+
connection.ping(reconnect=True)
245+
except Exception as exc:
246+
logger.warning("Connection %s is dead, removing: %s", connection_id, exc)
247+
self._close_connection_unlocked(connection_id, connection)
248+
raise DatabaseConnectionError(
249+
f"Connection {connection_id} is no longer valid"
250+
) from exc
251+
try:
252+
cursor = connection.cursor()
253+
except Exception as exc:
254+
logger.warning("Connection %s could not create a cursor: %s", connection_id, exc)
255+
self._close_connection_unlocked(connection_id, connection)
256+
raise DatabaseConnectionError(
257+
f"Connection {connection_id} is no longer valid"
258+
) from exc
259+
try:
260+
# Exceptions from the caller's SQL body must propagate without
261+
# evicting a healthy connection from the registry.
262+
yield cursor
263+
finally:
264+
cursor.close()
203265

204266
def execute_query(self, connection_id: str, query: str, params: Optional[tuple] = None) -> List[Dict[str, Any]]:
205267
"""

0 commit comments

Comments
 (0)