Closing pooled connections
This commit is contained in:
parent
12f36c5113
commit
8a9e3884fe
@ -13,10 +13,11 @@ def GetBerths(token):
|
||||
# TODO: validate token
|
||||
|
||||
try:
|
||||
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
data = commands.query("SELECT id, name, participant_id, `lock`, created, modified FROM berth ORDER BY name", model=model.Berth)
|
||||
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
data = commands.query("SELECT id, name, participant_id, `lock`, created, modified FROM berth ORDER BY name", model=model.Berth)
|
||||
pooledConnection.close()
|
||||
|
||||
except Exception as ex:
|
||||
logging.error(ex)
|
||||
print(ex)
|
||||
|
||||
@ -12,10 +12,11 @@ def GetUser(options):
|
||||
try:
|
||||
if "password" in options and "username" in options:
|
||||
hash = bcrypt.hashpw(options["password"].encode('utf-8'), bcrypt.gensalt( 12 )).decode('utf8')
|
||||
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
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 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")):
|
||||
|
||||
@ -14,12 +14,14 @@ def GetParticipant(options):
|
||||
# TODO: validate token
|
||||
|
||||
try:
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
if "user_id" in options and options["user_id"]:
|
||||
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.flags as flags, p.created as created, p.modified as modified FROM p INNER JOIN user u WHERE u.participant_id = p.id and u.id = ?userid?", model=model.Participant, param={"userid" : options["user_id"]})
|
||||
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.flags as flags, p.created as created, p.modified as modified FROM 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 p.id as id, p.name as name, p.street as street, p.postal_code as postal_code, p.city as city, p.flags as flags, p.created as created, p.modified as modified FROM participant p ORDER BY p.name", model=model.Participant)
|
||||
|
||||
pooledConnection.close()
|
||||
|
||||
except Exception as ex:
|
||||
logging.error(ex)
|
||||
print(ex)
|
||||
|
||||
@ -15,11 +15,13 @@ def GetShipcalls(options):
|
||||
|
||||
try:
|
||||
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
data = commands.query("SELECT id, ship_id, type, eta, voyage, etd, arrival_berth_id, departure_berth_id, tug_required, pilot_required, " +
|
||||
"flags, pier_side, bunkering, replenishing, draft, tidal_window_from, tidal_window_to, rain_sensitive_cargo, recommended_tugs, " +
|
||||
"created, modified FROM shipcall WHERE eta IS NULL OR eta >= DATE(NOW() - INTERVAL 2 DAY) " +
|
||||
"ORDER BY eta", model=model.Shipcall)
|
||||
pooledConnection.close()
|
||||
|
||||
except Exception as ex:
|
||||
logging.error(ex)
|
||||
@ -41,7 +43,9 @@ def PostShipcalls(schemaModel):
|
||||
|
||||
# This creates a *new* entry
|
||||
try:
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
|
||||
query = "INSERT INTO shipcall ("
|
||||
isNotFirst = False
|
||||
@ -64,8 +68,10 @@ def PostShipcalls(schemaModel):
|
||||
query += ")"
|
||||
|
||||
commands.execute(query, schemaModel)
|
||||
|
||||
new_id = commands.execute_scalar("select last_insert_id()")
|
||||
|
||||
pooledConnection.close()
|
||||
|
||||
return json.dumps({"id" : new_id}), 201
|
||||
|
||||
except Exception as ex:
|
||||
@ -84,7 +90,9 @@ def PutShipcalls(schemaModel):
|
||||
|
||||
# This updates an *existing* entry
|
||||
try:
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
|
||||
query = "UPDATE shipcall SET "
|
||||
isNotFirst = False
|
||||
@ -100,6 +108,8 @@ def PutShipcalls(schemaModel):
|
||||
|
||||
affected_rows = commands.execute(query, param=schemaModel)
|
||||
|
||||
pooledConnection.close()
|
||||
|
||||
if affected_rows == 1:
|
||||
return json.dumps({"id" : schemaModel["id"]}), 200
|
||||
|
||||
|
||||
@ -14,8 +14,10 @@ def GetShips(token):
|
||||
|
||||
try:
|
||||
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
data = commands.query("SELECT id, name, imo, callsign, participant_id, length, width, created, modified FROM ship ORDER BY name", model=model.Ship)
|
||||
pooledConnection.close()
|
||||
|
||||
except Exception as ex:
|
||||
logging.error(ex)
|
||||
|
||||
@ -15,9 +15,12 @@ def GetTimes(options):
|
||||
# TODO: validate token
|
||||
|
||||
try:
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
data = commands.query("SELECT id, start_planned, end_planned, duration_planned, start_actual, end_actual, duration_actual, shipcall_id, participant_id, created, modified FROM times " +
|
||||
"WHERE times.shipcall_id = ?scid?", model=model.Times, param={"scid" : options["shipcall_id"]})
|
||||
pooledConnection.close()
|
||||
|
||||
except Exception as ex:
|
||||
logging.error(ex)
|
||||
@ -40,7 +43,9 @@ def PostTimes(schemaModel):
|
||||
|
||||
# This creates a *new* entry
|
||||
try:
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
|
||||
query = "INSERT INTO times ("
|
||||
isNotFirst = False
|
||||
@ -63,8 +68,10 @@ def PostTimes(schemaModel):
|
||||
query += ")"
|
||||
|
||||
commands.execute(query, schemaModel)
|
||||
|
||||
new_id = commands.execute_scalar("select last_insert_id()")
|
||||
|
||||
pooledConnection.close()
|
||||
|
||||
return json.dumps({"id" : new_id}), 201
|
||||
|
||||
except Exception as ex:
|
||||
@ -83,7 +90,9 @@ def PutTimes(schemaModel):
|
||||
|
||||
# This updates an *existing* entry
|
||||
try:
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
|
||||
pooledConnection = local_db.getPoolConnection()
|
||||
commands = pydapper.using(pooledConnection)
|
||||
|
||||
query = "UPDATE times SET "
|
||||
isNotFirst = False
|
||||
@ -99,6 +108,8 @@ def PutTimes(schemaModel):
|
||||
|
||||
affected_rows = commands.execute(query, param=schemaModel)
|
||||
|
||||
pooledConnection.close()
|
||||
|
||||
if affected_rows == 1:
|
||||
return json.dumps({"id" : schemaModel["id"]}), 200
|
||||
|
||||
@ -118,8 +129,11 @@ def DeleteTimes(options):
|
||||
|
||||
"""
|
||||
try:
|
||||
commands = pydapper.using(local_db.connection_pool)
|
||||
|
||||
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
|
||||
|
||||
@ -8,7 +8,6 @@ connection_pool = None
|
||||
|
||||
def initPool():
|
||||
try:
|
||||
global connection_pool
|
||||
|
||||
config_path = './src/server/BreCal/connection_data.json'
|
||||
print (os.getcwd())
|
||||
@ -19,14 +18,20 @@ def initPool():
|
||||
f = open(config_path);
|
||||
connection_data = json.load(f)
|
||||
|
||||
connection_pool = mysql.connector.connect(**connection_data)
|
||||
conn_from_pool = mysql.connector.connect(**connection_data)
|
||||
|
||||
commands = pydapper.using(connection_pool)
|
||||
commands = pydapper.using(conn_from_pool)
|
||||
data = commands.query("SELECT id from `user`")
|
||||
print("DB connection successful")
|
||||
|
||||
conn_from_pool.close()
|
||||
except mysql.connector.PoolError as e:
|
||||
logging.error(f"Failed to create connection pool: {e}")
|
||||
print(e)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print(e)
|
||||
|
||||
def getPoolConnection():
|
||||
config_path = './src/server/BreCal/connection_data.json'
|
||||
f = open(config_path);
|
||||
connection_data = json.load(f)
|
||||
return mysql.connector.connect(**connection_data)
|
||||
Reference in New Issue
Block a user