mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-02 17:48:36 -04:00
Adding @nested_transactional for transactions that require more than one
db update/insert. Using a savepoint for the multiple transactions allows us to rollback if there is an error when executing the second db transaction. However, this does add a bit of complexity. Developers need to manage the db session when calling multiple nested tranactions. Unit tests have been added to test this functionality and some end to end tests have been done to make sure all transactions are rollback if there is an exception while executing the transaction.
This commit is contained in:
@@ -18,6 +18,23 @@ def transactional(func):
|
||||
return commit_or_rollback
|
||||
|
||||
|
||||
def nested_transactional(func):
|
||||
# This creates a save point for the nested transaction.
|
||||
# You must manage the commit or rollback from outer most call of the nested of the transactions.
|
||||
@wraps(func)
|
||||
def commit_or_rollback(*args, **kwargs):
|
||||
try:
|
||||
db.session.begin_nested()
|
||||
res = func(*args, **kwargs)
|
||||
db.session.commit()
|
||||
return res
|
||||
except Exception:
|
||||
db.session.rollback()
|
||||
raise
|
||||
|
||||
return commit_or_rollback
|
||||
|
||||
|
||||
class VersionOptions():
|
||||
|
||||
def __init__(self, model_class, history_class=None, must_write_history=True):
|
||||
|
||||
Reference in New Issue
Block a user