22Database connection manager for DBJavaGenix MCP tools
33"""
44import uuid
5+ import threading
56from typing import Dict , List , Any , Optional
67import pymysql
78import 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