@@ -100,18 +100,21 @@ def __init__(self, **kwargs):
100100 #self._connect()
101101
102102 def _connect (self ):
103- for i in range (Settings .app ['retries' ]['db_connection' ]):
103+ retries = Settings .app ['retries' ]['db_connection' ]
104+ for i in range (retries ):
104105 try :
105106 self .async_mode = True if 'async_mode' in self ._connection_params else False
106107 self .client = psycopg2 .connect (self ._connection_params ['dsn' ], async_ = int (self .async_mode ))
107108 if self .async_mode :
108109 Connection .__wait_for_completion (self .client )
109110 self .acursor = self .client .cursor ()
111+ self ._last_usage_timestamp = time .time ()
110112 break
111113 except psycopg2 .OperationalError :
112114 self .__logger .warning ('Error connecting to the db server' , exc_info = True )
113115 time .sleep (0.1 )
114- self ._last_usage_timestamp = time .time ()
116+ else :
117+ raise UnitDbConnectionError (f'Error connecting to the db server after { retries } tries' )
115118
116119 def _refresh_conn_on_every_usage (self ):
117120 return "socket_reusability" in self ._connection_params and \
@@ -129,7 +132,10 @@ def wait_for_completion(self):
129132 @classmethod
130133 def connect (cls , alias = DEFAULT_CONNECTION_NAME , ** kwargs ):
131134 if alias not in cls .__connections :
132- cls .__connections [alias ] = {"connection" : cls (** kwargs ), "args" : kwargs }
135+ logging .getLogger (__name__ ).debug ("Creating connection object..." )
136+ connection = cls (** kwargs )
137+ logging .getLogger (__name__ ).debug (f"Connection object created." )
138+ cls .__connections [alias ] = {"connection" : connection , "args" : kwargs }
133139 return cls .use (alias )
134140
135141 @classmethod
0 commit comments