From 34d5054075b9e2579df72fc722f0d9589551b8d0 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 17 Jun 2020 14:09:39 +0100 Subject: [PATCH] Commit transactions as soon no longer needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We think that holding open database transactions while we go and do something else is causing us to have poor performance. Because we’re not serialising everything as soon as we pull it out of the database we can guarantee that we don’t need to go back to the database again. So let’s see if explicitly closing the transaction helps with performance. --- app/authentication/auth.py | 2 ++ app/notifications/validators.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/authentication/auth.py b/app/authentication/auth.py index 927fd197e..e61990749 100644 --- a/app/authentication/auth.py +++ b/app/authentication/auth.py @@ -8,6 +8,7 @@ from sqlalchemy.exc import DataError from sqlalchemy.orm.exc import NoResultFound from gds_metrics import Histogram +from app import db from app.dao.services_dao import dao_fetch_service_by_id from app.dao.api_key_dao import get_model_api_keys from app.serialised_models import ( @@ -123,6 +124,7 @@ def requires_auth(): try: service = get_service_model(issuer) service.api_keys = get_api_keys_models(issuer) + db.session.commit() except DataError: raise AuthError("Invalid token: service id is not the right data type", 403) except NoResultFound: diff --git a/app/notifications/validators.py b/app/notifications/validators.py index 40c14b86d..ea939a59e 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -8,6 +8,7 @@ from notifications_utils.recipients import ( ) from notifications_utils.clients.redis import rate_limit_cache_key, daily_limit_cache_key +from app import db from app.dao import services_dao, templates_dao from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id from app.models import ( @@ -160,7 +161,10 @@ def get_template_dict(template_id, service_id): raise BadRequestError(message=message, fields=[{'template': message}]) - return template_schema.dump(fetched_template).data + template_dict = template_schema.dump(fetched_template).data + + db.session.commit() + return template_dict def get_template_model(template_id, service_id):