2525
2626def db_inputs (connection_type ):
2727 """
28- a function to determine the type of database the user wants to work with and
29- selects the corresponding connection to the db
28+ a function to determine the type of database the user wants to work with and
29+ selects the corresponding connection to the db
3030
31- Args:
32- connection_type: type of db we are working with
31+ Args:
32+ connection_type: type of db we are working with
3333
34- Returns:
35- corresponding command to connect to the db
36- """
34+ Returns:
35+ corresponding command to connect to the db
36+ """
3737 return {
3838 "postgres" : 'postgres+psycopg2://{0}:{1}@{2}:{3}/{4}' .format (USER , PASSWORD , HOST , PORT , DATABASE ),
3939 "mysql" : 'mysql://{0}:{1}@{2}:{3}/{4}' .format (USER , PASSWORD , HOST , PORT , DATABASE ),
@@ -43,25 +43,22 @@ def db_inputs(connection_type):
4343
4444def create_connection ():
4545 """
46- a function to create connections to db, it retries 100 times if connection returned an error
46+ a function to create connections to db with pessimistic approach
4747
4848 Returns:
4949 connection if success otherwise False
5050 """
5151 try :
52- for _ in range (0 , 100 ):
53- try :
54- db_engine = create_engine (
55- db_inputs (DB ),
56- connect_args = {
57- 'check_same_thread' : False
58- }
59- )
60- Session = sessionmaker (bind = db_engine )
61- session = Session ()
62- return session
63- except Exception :
64- time .sleep (0.1 )
52+ db_engine = create_engine (
53+ db_inputs (DB ),
54+ connect_args = {
55+ 'check_same_thread' : False
56+ },
57+ pool_pre_ping = True
58+ )
59+ Session = sessionmaker (bind = db_engine )
60+ session = Session ()
61+ return session
6562 except Exception :
6663 warn (messages ("database_connect_fail" ))
6764 return False
@@ -196,17 +193,17 @@ def submit_temp_logs_to_db(log):
196193
197194def find_temp_events (target , module_name , scan_unique_id , event_name ):
198195 """
199- select all events by scan_unique id, target, module_name
196+ select all events by scan_unique id, target, module_name
200197
201- Args:
202- target: target
203- module_name: module name
204- scan_unique_id: unique scan identifier
205- event_name: event_name
198+ Args:
199+ target: target
200+ module_name: module name
201+ scan_unique_id: unique scan identifier
202+ event_name: event_name
206203
207- Returns:
208- an array with JSON events or an empty array
209- """
204+ Returns:
205+ an array with JSON events or an empty array
206+ """
210207 session = create_connection ()
211208 try :
212209 for _ in range (1 , 100 ):
0 commit comments