1313from .cursor import Cursor
1414from .error import DatabaseError , OperationalError
1515from .helper import ConnectionHelper
16+ from .retry import RetryConfig , execute_with_retry
1617
1718_logger = logging .getLogger (__name__ )
1819
@@ -55,6 +56,10 @@ def __init__(
5556 if not self ._mode and mode :
5657 self ._mode = mode
5758
59+ # Retry behavior for transient system-level PyMongo failures.
60+ # These kwargs are consumed by PyMongoSQL and are not passed to MongoClient.
61+ self ._retry_config = RetryConfig .from_kwargs (kwargs )
62+
5863 # Extract commonly used parameters for backward compatibility
5964 self ._host = host or "localhost"
6065 self ._port = port or 27017
@@ -109,7 +114,11 @@ def _connect(self) -> None:
109114 self ._client = MongoClient (** self ._pymongo_params )
110115
111116 # Test connection
112- self ._client .admin .command ("ping" )
117+ execute_with_retry (
118+ lambda : self ._client .admin .command ("ping" ),
119+ self ._retry_config ,
120+ "initial MongoDB ping" ,
121+ )
113122
114123 # Initialize the database according to explicit parameter or client's default
115124 # This may raise OperationalError if no database could be determined; allow it to bubble up
@@ -179,6 +188,11 @@ def mode(self) -> str:
179188 """Get the specified mode"""
180189 return self ._mode
181190
191+ @property
192+ def retry_config (self ) -> RetryConfig :
193+ """Get retry configuration used for transient system-level errors."""
194+ return self ._retry_config
195+
182196 def use_database (self , database_name : str ) -> None :
183197 """Switch to a different database"""
184198 if self ._client is None :
@@ -332,15 +346,23 @@ def _start_session(self, **kwargs) -> ClientSession:
332346 if self ._client is None :
333347 raise OperationalError ("No active connection" )
334348
335- session = self ._client .start_session (** kwargs )
349+ session = execute_with_retry (
350+ lambda : self ._client .start_session (** kwargs ),
351+ self ._retry_config ,
352+ "start MongoDB session" ,
353+ )
336354 self ._session = session
337355 _logger .info ("Started new MongoDB session" )
338356 return session
339357
340358 def _end_session (self ) -> None :
341359 """End the current session (internal method)"""
342360 if self ._session is not None :
343- self ._session .end_session ()
361+ execute_with_retry (
362+ lambda : self ._session .end_session (),
363+ self ._retry_config ,
364+ "end MongoDB session" ,
365+ )
344366 self ._session = None
345367 _logger .info ("Ended MongoDB session" )
346368
@@ -357,7 +379,11 @@ def _start_transaction(self, **kwargs) -> None:
357379 if self ._session is None :
358380 raise OperationalError ("No active session" )
359381
360- self ._session .start_transaction (** kwargs )
382+ execute_with_retry (
383+ lambda : self ._session .start_transaction (** kwargs ),
384+ self ._retry_config ,
385+ "start MongoDB transaction" ,
386+ )
361387 self ._in_transaction = True
362388 self ._autocommit = False
363389 _logger .info ("Started MongoDB transaction" )
@@ -370,7 +396,11 @@ def _commit_transaction(self) -> None:
370396 if not self ._session .in_transaction :
371397 raise OperationalError ("No active transaction to commit" )
372398
373- self ._session .commit_transaction ()
399+ execute_with_retry (
400+ lambda : self ._session .commit_transaction (),
401+ self ._retry_config ,
402+ "commit MongoDB transaction" ,
403+ )
374404 self ._in_transaction = False
375405 self ._autocommit = True
376406 _logger .info ("Committed MongoDB transaction" )
@@ -383,7 +413,11 @@ def _abort_transaction(self) -> None:
383413 if not self ._session .in_transaction :
384414 raise OperationalError ("No active transaction to abort" )
385415
386- self ._session .abort_transaction ()
416+ execute_with_retry (
417+ lambda : self ._session .abort_transaction (),
418+ self ._retry_config ,
419+ "abort MongoDB transaction" ,
420+ )
387421 self ._in_transaction = False
388422 self ._autocommit = True
389423 _logger .info ("Aborted MongoDB transaction" )
@@ -460,7 +494,11 @@ def test_connection(self) -> bool:
460494 """Test if the connection is alive"""
461495 try :
462496 if self ._client :
463- self ._client .admin .command ("ping" )
497+ execute_with_retry (
498+ lambda : self ._client .admin .command ("ping" ),
499+ self ._retry_config ,
500+ "connection health check ping" ,
501+ )
464502 return True
465503 return False
466504 except Exception as e :
0 commit comments