mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
New strategy for transaction management.
Introduce a contextmanger function to handle exceptions and nested transactions. Using the nested_transaction will start a nested transaction with `db.session.begin_nested`, once the nested transaction is complete the commit will happen. `@transactional` has been updated to commit unless in a nested transaction.
This commit is contained in:
@@ -4,10 +4,9 @@ from datetime import datetime
|
||||
from flask import Blueprint, current_app, jsonify, request
|
||||
from notifications_utils.letter_timings import letter_can_be_cancelled
|
||||
from notifications_utils.timezones import convert_utc_to_bst
|
||||
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import db
|
||||
from app.aws import s3
|
||||
from app.config import QueueNames
|
||||
from app.dao import fact_notification_status_dao, notifications_dao
|
||||
@@ -19,7 +18,7 @@ from app.dao.api_key_dao import (
|
||||
save_model_api_key,
|
||||
)
|
||||
from app.dao.broadcast_service_dao import set_broadcast_service_type
|
||||
from app.dao.dao_utils import dao_rollback
|
||||
from app.dao.dao_utils import dao_rollback, nested_transaction
|
||||
from app.dao.date_util import get_financial_year
|
||||
from app.dao.fact_notification_status_dao import (
|
||||
fetch_monthly_template_usage_for_service,
|
||||
@@ -255,13 +254,9 @@ def create_service():
|
||||
# unpack valid json into service object
|
||||
valid_service = Service.from_json(data)
|
||||
|
||||
try:
|
||||
with nested_transaction():
|
||||
dao_create_service(valid_service, user)
|
||||
set_default_free_allowance_for_service(service=valid_service, year_start=None)
|
||||
db.session.commit()
|
||||
except SQLAlchemyError as e:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
|
||||
return jsonify(data=service_schema.dump(valid_service).data), 201
|
||||
|
||||
|
||||
Reference in New Issue
Block a user