@@ -38,13 +38,11 @@ def _on_init_done(self, future):
3838 future .result ()
3939 except Exception as e :
4040 logging .error ("Initialization failed: %s" , e , exc_info = True )
41- raise
42- self ._initialized = True
43- self ._initialized_event .set ()
41+ raise e from e
4442
4543 async def _async_init (self ):
4644 async with self ._lock :
47- await self ._initialize_connection ()
45+ self ._initialize_connection ()
4846 self ._initialized = True
4947 self ._initialized_event .set ()
5048
@@ -54,11 +52,10 @@ async def __aenter__(self):
5452
5553 async def __aexit__ (self , exc_type , exc_value , traceback ):
5654 await self ._initialized_event .wait ()
57- self .close_connection ()
5855 async with self ._lock :
5956 self ._cleanup_instance ()
6057
61- async def _initialize_connection (self ):
58+ def _initialize_connection (self ):
6259 try :
6360 self ._connection_string = Secrets .get_secret (
6461 "MONGODB_CONNECTION_STRING"
@@ -81,6 +78,9 @@ async def _initialize_connection(self):
8178 ) from e
8279
8380 def _cleanup_instance (self ):
81+ if hasattr (self , '_client' ):
82+ self .client .close ()
83+ logger .info ("Connection closed for %s" , self .__class__ )
8484 self ._instances .pop (self .__class__ , None )
8585
8686 @property
@@ -114,8 +114,3 @@ def collection(self, value):
114114 if not getattr (self , '_initialized' , False ):
115115 raise RuntimeError ("Repository not initialized yet" )
116116 self ._collection = value
117-
118- def close_connection (self ):
119- if hasattr (self , '_client' ):
120- self .client .close ()
121- logger .info ("Connection closed for %s" , self .__class__ )
0 commit comments