99 ArgumentError ,
1010 UnboundExecutionError ,
1111 OperationalError ,
12- TimeoutError ,
12+ TimeoutError as SQLTimeoutError ,
1313 DatabaseError ,
1414)
1515from flask_sqlalchemy import SQLAlchemy
1818from common .pyportal_common .db_handlers .db_conn_manager import (
1919 DataBaseConnectionHandler ,
2020)
21-
22- # from sqlalchemy.exc import (
23- # SQLAlchemyError,
24- # ArgumentError,
25- # UnboundExecutionError,
26- # OperationalError,
27- # TimeoutError,
28- # DatabaseError,
29- # )
3021from sqlalchemy .orm import sessionmaker
3122
3223
@@ -48,11 +39,13 @@ def create_pool_of_connections(
4839 ) -> Union [QueuePool , None ]:
4940 try :
5041 self .cmn_logger .info (
51- f"Creating Pool of connections for Service ==>[{ self .cmn_logger .name } ] :: [STARTED]"
42+ f"Creating Pool of connections for Service "
43+ f"==>[{ self .cmn_logger .name } ] :: [STARTED]"
5244 )
5345 if not db_engine :
5446 self .cmn_logger .info (
55- f"Created Pool of connections for Service ==>[{ self .cmn_logger .name } ] :: [FAILED]"
47+ f"Created Pool of connections for Service "
48+ f"==>[{ self .cmn_logger .name } ] :: [FAILED]"
5649 )
5750 return None
5851 else :
@@ -64,12 +57,14 @@ def create_pool_of_connections(
6457 max_overflow = self .db_pool_max_overflow ,
6558 )
6659 self .cmn_logger .info (
67- f"Created Pool of connections for Service ==>[{ self .cmn_logger .name } ] :: [SUCCESS]"
60+ f"Created Pool of connections for Service "
61+ f"==>[{ self .cmn_logger .name } ] :: [SUCCESS]"
6862 )
6963 return pool_of_db_connections
7064 except SQLAlchemyError as ex :
7165 self .cmn_logger .error (
72- f"{ ex } error occurred while Creating Pool of connections for Service \t Line No:: { sys .exc_info ()[2 ].tb_lineno } "
66+ f"{ ex } error occurred while Creating Pool of connections "
67+ f"for Service \t Line No:: { sys .exc_info ()[2 ].tb_lineno } "
7368 )
7469 return None
7570
@@ -84,91 +79,96 @@ def create_session_maker_bind_to_db_engine(
8479 return None
8580 else :
8681 self .cmn_logger .info (
87- "Creating session maker for managing database sessions/interactions :: [STARTED]"
82+ "Creating session maker for managing database "
83+ "sessions/interactions :: [STARTED]"
8884 )
8985 self .session_maker = sessionmaker (bind = db_engine )
9086 self .cmn_logger .info (
9187 f"Created session using Pool_info :: { connection_pool } "
9288 )
9389 self .cmn_logger .info (
94- f"Created a session maker for database interactions { self .session_maker } :: [SUCCESS]"
90+ f"Created a session maker for database interactions "
91+ f"{ self .session_maker } :: [SUCCESS]"
9592 )
9693 return self .session_maker
9794 except (
9895 ArgumentError ,
9996 UnboundExecutionError ,
10097 OperationalError ,
101- TimeoutError ,
98+ SQLTimeoutError ,
10299 DatabaseError ,
103100 ) as ex :
104101 self .cmn_logger .error (
105- f"{ ex } error occurred while Creating session using pool of connections \t Line No:: { sys .exc_info ()[2 ].tb_lineno } "
102+ f"{ ex } error occurred while Creating session using pool "
103+ f"of connections \t Line No:: { sys .exc_info ()[2 ].tb_lineno } "
106104 )
107105 return None
108106
109- # def rollback_session(self, session_instance: sqlalchemy.orm.Session) -> Union[bool, None]:
110- # try:
111- # if session_instance:
112- # self.cmn_logger.info(f"Rollback the session {session_instance}:: ")
113- # session_instance.rollback()
114- # self.cmn_logger.info(f"Rollback the session {session_instance}:: ")
115- # return True
116- # except Exception as ex:
117- # self.cmn_logger.info(f"Error occurred :: {ex}\tLine No:: {sys.exc_info()[2].tb_lineno}")
118- # print(f"Error occurred :: {ex}\tLine No:: {sys.exc_info()[2].tb_lineno}")
119- # return False
120-
121- def get_session_from_session_maker (self ) -> Union [sqlalchemy .orm .Session , None ]:
107+ def get_session_from_session_maker (
108+ self
109+ ) -> Union [sqlalchemy .orm .Session , None ]:
122110 try :
123111 if self .session_maker is None :
124112 self .cmn_logger .info (
125- "Fetching session from sessions of connections :: [FAILED]"
113+ "Fetching session from sessions of connections "
114+ ":: [FAILED]"
126115 )
127116 return None
128117 else :
129- self .display_pool_info (
130- connection_pool = self .session_maker .kw ["bind" ].pool
131- )
118+ # Get pool from engine bound to sessionmaker
119+ # pylint: disable=no-member
120+ bind_engine = getattr (self .session_maker , 'bind' , None )
121+ if bind_engine and hasattr (bind_engine , 'pool' ):
122+ self .display_pool_info (
123+ connection_pool = bind_engine .pool
124+ )
132125 self .cmn_logger .info (
133- "Fetching session from sessions of connections :: [STARTED]"
126+ "Fetching session from sessions of connections "
127+ ":: [STARTED]"
134128 )
135129 session_instance = self .session_maker ()
136130
137131 self .cmn_logger .info (
138- "Fetching session %s from sessions of connections:: [SUCCESS]" ,
132+ "Fetching session %s from sessions of connections "
133+ ":: [SUCCESS]" ,
139134 session_instance ,
140135 )
141136 return session_instance
142137 except (
143138 ArgumentError ,
144139 UnboundExecutionError ,
145140 OperationalError ,
146- TimeoutError ,
141+ SQLTimeoutError ,
147142 DatabaseError ,
148143 ) as ex :
149144 self .cmn_logger .error (
150- f"{ ex } error occurred while Fetching session from sessions of connections \t Line No:: { sys .exc_info ()[2 ].tb_lineno } "
145+ f"{ ex } error occurred while Fetching session from "
146+ f"sessions of connections \t Line No:: "
147+ f"{ sys .exc_info ()[2 ].tb_lineno } "
151148 )
152149 return None
153150
154151 def close_session (
155152 self , session_instance : sqlalchemy .orm .Session
156153 ) -> Union [bool , None ]:
157154 try :
158- self .cmn_logger .info (f"Closing the session { session_instance } :: " )
155+ self .cmn_logger .info (
156+ f"Closing the session { session_instance } :: "
157+ )
159158 if not session_instance .is_active :
160- pass
161159 self .cmn_logger .info (
162- f"Session of Session-Id { session_instance } which is not active or already closed:: [SUCCESS]"
160+ f"Session of Session-Id { session_instance } which is "
161+ f"not active or already closed:: [SUCCESS]"
163162 )
164163 else :
165164 session_instance .close ()
166165 self .cmn_logger .info (
167166 f"Closed session of Session-Id { session_instance } :: "
168167 )
169168 return True
170- except Exception as ex :
169+ except ( SQLAlchemyError , AttributeError , RuntimeError ) as ex :
171170 self .cmn_logger .error (
172- f"{ ex } error occurred while Closing the session \t Line No:: { sys .exc_info ()[2 ].tb_lineno } "
171+ f"{ ex } error occurred while Closing the session "
172+ f"\t Line No:: { sys .exc_info ()[2 ].tb_lineno } "
173173 )
174174 return None
0 commit comments