git_brcal/src/server/BreCal/impl/berths.py
Daniel Schick 946afee3c1 Initializing pool connection variable with None.
Release pool connection handle und all circumstances especially also when a query fails
before the call is finished. This should avoid connection starvation.
2025-11-12 13:55:58 +01:00

45 lines
1.4 KiB
Python

import json
import logging
import pydapper
from ..schemas import model
from .. import local_db
def GetBerths(options):
"""
No parameters, gets all entries
"""
pooledConnection = None
try:
pooledConnection = local_db.getPoolConnection()
commands = pydapper.using(pooledConnection)
# only load berths to ports that the participant is assigned to
if "participant_id" in options:
query = ("SELECT id, name, `lock`, owner_id, port_id, authority_id, created, modified, deleted FROM berth WHERE " +
"deleted = 0 AND + "
"port_id IN (SELECT port_id FROM participant_port_map WHERE participant_id = %d) " +
"ORDER BY name") % (options["participant_id"])
else:
query = ("SELECT id, name, `lock`, owner_id, port_id, authority_id, created, modified, deleted FROM berth WHERE " +
"deleted = 0 ORDER BY name")
data = commands.query(query, model=model.Berth)
return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'}
except Exception as ex:
logging.error(ex)
print(ex)
result = {}
result["error_field"] = "call failed"
return json.dumps(result), 500
finally:
if pooledConnection is not None:
pooledConnection.close()