From e3017349b0c61b6420b7209b6aa5e15fa619ab8e Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Mon, 5 Feb 2024 09:48:34 +0100 Subject: [PATCH] added finally statement to close pooledConnection under all circumstances --- src/server/BreCal/impl/berths.py | 6 ++++-- src/server/BreCal/impl/login.py | 5 ++++- src/server/BreCal/impl/participant.py | 8 +++++--- src/server/BreCal/impl/shipcalls.py | 21 +++++++++++---------- src/server/BreCal/impl/ships.py | 8 ++++++-- src/server/BreCal/impl/times.py | 19 +++++++++++++------ src/server/BreCal/impl/user.py | 5 +++-- 7 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/server/BreCal/impl/berths.py b/src/server/BreCal/impl/berths.py index d41025c..3a65089 100644 --- a/src/server/BreCal/impl/berths.py +++ b/src/server/BreCal/impl/berths.py @@ -16,7 +16,7 @@ def GetBerths(token): pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) data = commands.query("SELECT id, name, `lock`, owner_id, authority_id, created, modified, deleted FROM berth WHERE deleted = 0 ORDER BY name", model=model.Berth) - pooledConnection.close() + return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(ex) @@ -25,7 +25,9 @@ def GetBerths(token): result["message"] = "call failed" return json.dumps(result), 500 - return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() diff --git a/src/server/BreCal/impl/login.py b/src/server/BreCal/impl/login.py index 5f1847b..cbcf864 100644 --- a/src/server/BreCal/impl/login.py +++ b/src/server/BreCal/impl/login.py @@ -16,7 +16,6 @@ def GetUser(options): commands = pydapper.using(pooledConnection) data = commands.query("SELECT id, participant_id, first_name, last_name, user_name, user_email, user_phone, password_hash, api_key, created, modified FROM user WHERE user_name = ?username? OR user_email = ?username?", model=model.User, param={"username" : options["username"]}) - pooledConnection.close() # print(data) if len(data) == 1: if bcrypt.checkpw(options["password"].encode("utf-8"), bytes(data[0].password_hash, "utf-8")): @@ -49,6 +48,10 @@ def GetUser(options): result["message"] = "call failed: " + str(ex) return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() + # $2b$12$uWLE0r32IrtCV30WkMbVwOdltgeibymZyYAf4ZnQb2Bip8hrkGGwG # $2b$12$.vEapj9xU8z0RK0IpIGeYuRIl0ktdMt4XdJQBhVn.3K2hmvm7qD3y # $2b$12$yL3PiseU70ciwEuMVM4OtuMwR6tNuIT9vvBiBG/uyMrPxa16E2Zqu \ No newline at end of file diff --git a/src/server/BreCal/impl/participant.py b/src/server/BreCal/impl/participant.py index be1d7ec..aad0def 100644 --- a/src/server/BreCal/impl/participant.py +++ b/src/server/BreCal/impl/participant.py @@ -11,7 +11,6 @@ def GetParticipant(options): options["user_id"]: **Id of user**. *Example: 2*. User id returned by login call. """ - # TODO: validate token try: pooledConnection = local_db.getPoolConnection() @@ -20,7 +19,8 @@ def GetParticipant(options): data = commands.query("SELECT p.id as id, p.name as name, p.street as street, p.postal_code as postal_code, p.city as city, p.type as type, p.flags as flags, p.created as created, p.modified as modified, p.deleted as deleted FROM participant p INNER JOIN user u WHERE u.participant_id = p.id and u.id = ?userid?", model=model.Participant, param={"userid" : options["user_id"]}) else: data = commands.query("SELECT id, name, street, postal_code, city, type, flags, created, modified, deleted FROM participant p ORDER BY p.name", model=model.Participant) - pooledConnection.close() + + return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(ex) @@ -29,5 +29,7 @@ def GetParticipant(options): result["message"] = "call failed" return json.dumps("call failed"), 500 - return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() diff --git a/src/server/BreCal/impl/shipcalls.py b/src/server/BreCal/impl/shipcalls.py index e032f4f..f95934c 100644 --- a/src/server/BreCal/impl/shipcalls.py +++ b/src/server/BreCal/impl/shipcalls.py @@ -13,8 +13,6 @@ def GetShipcalls(options): No parameters, gets all entries """ - # TODO: validate token - try: pooledConnection = local_db.getPoolConnection() @@ -42,7 +40,7 @@ def GetShipcalls(options): pa = model.Participant_Assignment(record["participant_id"], record["type"]) shipcall.participants.append(pa) - pooledConnection.close() + return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(traceback.format_exc()) @@ -52,7 +50,9 @@ def GetShipcalls(options): result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} - return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() def PostShipcalls(schemaModel): @@ -120,8 +120,6 @@ def PostShipcalls(schemaModel): # apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=new_id) # new_id (last insert id) refers to the shipcall id - pooledConnection.close() - return json.dumps({"id" : new_id}), 201, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: @@ -132,6 +130,10 @@ def PostShipcalls(schemaModel): result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() + def PutShipcalls(schemaModel): """ @@ -203,9 +205,6 @@ def PutShipcalls(schemaModel): commands.execute(dquery, param={"existing_id" : elem["id"]}) # apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database - evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=schemaModel["id"]) # schemaModel["id"] refers to the shipcall id - - pooledConnection.close() return json.dumps({"id" : schemaModel["id"]}), 200 @@ -217,5 +216,7 @@ def PutShipcalls(schemaModel): result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} - + finally: + if pooledConnection is not None: + pooledConnection.close() diff --git a/src/server/BreCal/impl/ships.py b/src/server/BreCal/impl/ships.py index 4288d7b..12e10ab 100644 --- a/src/server/BreCal/impl/ships.py +++ b/src/server/BreCal/impl/ships.py @@ -17,7 +17,8 @@ def GetShips(token): pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) data = commands.query("SELECT id, name, imo, callsign, participant_id, length, width, is_tug, bollard_pull, eni, created, modified, deleted FROM ship ORDER BY name", model=model.Ship) - pooledConnection.close() + + return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(ex) @@ -26,7 +27,10 @@ def GetShips(token): result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} - return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() + diff --git a/src/server/BreCal/impl/times.py b/src/server/BreCal/impl/times.py index 56e4c3f..440434e 100644 --- a/src/server/BreCal/impl/times.py +++ b/src/server/BreCal/impl/times.py @@ -85,8 +85,6 @@ def PostTimes(schemaModel): # apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database 'shipcall' evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=schemaModel["shipcall_id"]) # every times data object refers to the 'shipcall_id' - pooledConnection.close() - return json.dumps({"id" : new_id}), 201, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: @@ -96,6 +94,10 @@ def PostTimes(schemaModel): result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() + def PutTimes(schemaModel): """ @@ -130,8 +132,6 @@ def PutTimes(schemaModel): # apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database 'shipcall' evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=schemaModel["shipcall_id"]) # every times data object refers to the 'shipcall_id' - pooledConnection.close() - # if affected_rows == 1: # this doesn't work as expected return json.dumps({"id" : schemaModel["id"]}), 200, {'Content-Type': 'application/json; charset=utf-8'} @@ -145,6 +145,10 @@ def PutTimes(schemaModel): result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close() + def DeleteTimes(options): """ :param options: A dictionary containing all the paramters for the Operations @@ -156,7 +160,6 @@ def DeleteTimes(options): pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) affected_rows = commands.execute("DELETE FROM times WHERE id = ?id?", param={"id" : options["id"]}) - pooledConnection.close() if affected_rows == 1: return json.dumps({"id" : options["id"]}), 200, {'Content-Type': 'application/json; charset=utf-8'} @@ -170,4 +173,8 @@ def DeleteTimes(options): print(ex) result = {} result["message"] = "call failed" - return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} \ No newline at end of file + return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} + + finally: + if pooledConnection is not None: + pooledConnection.close() \ No newline at end of file diff --git a/src/server/BreCal/impl/user.py b/src/server/BreCal/impl/user.py index 699e4bf..cd79f89 100644 --- a/src/server/BreCal/impl/user.py +++ b/src/server/BreCal/impl/user.py @@ -56,8 +56,6 @@ def PutUser(schemaModel): result["message"] = "old password invalid" return json.dumps(result), 400, {'Content-Type': 'application/json; charset=utf-8'} - pooledConnection.close() - return json.dumps({"id" : schemaModel["id"]}), 200 except Exception as ex: @@ -67,5 +65,8 @@ def PutUser(schemaModel): result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} + finally: + if pooledConnection is not None: + pooledConnection.close()