From 559d486b77bb9827d9419df3a3525940a7af1e4a Mon Sep 17 00:00:00 2001 From: ricolin Date: Tue, 22 Apr 2025 14:48:48 +0800 Subject: [PATCH] Check transaction before begin Allow using nested transaction if db session already starts root transaction. Check if DB transaction already starts in barbican kek rewrap. And use nested transaction if DB session already starts it's root transaction. Change-Id: Iabe26f842b3065bdb7a4aefe0dd7a4f0835d23cb --- .../templates/bin/_simple_crypto_kek_rewrap.py.tpl | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/barbican/templates/bin/_simple_crypto_kek_rewrap.py.tpl b/barbican/templates/bin/_simple_crypto_kek_rewrap.py.tpl index 11ea3d01c0..61a14ec397 100644 --- a/barbican/templates/bin/_simple_crypto_kek_rewrap.py.tpl +++ b/barbican/templates/bin/_simple_crypto_kek_rewrap.py.tpl @@ -54,7 +54,10 @@ class KekRewrap(object): ) def rewrap_kek(self, project, kek): - with self.db_session.begin(): + db_begin_fn = self.db_session.begin_nested if ( + self.db_session.in_transaction()) else self.db_session.begin + with db_begin_fn(): + plugin_meta = kek.plugin_meta # try to unwrap with the target kek, and if successful, skip @@ -90,7 +93,9 @@ class KekRewrap(object): def get_keks_for_project(self, project): keks = [] - with self.db_session.begin() as transaction: + db_begin_fn = self.db_session.begin_nested if ( + self.db_session.in_transaction()) else self.db_session.begin + with db_begin_fn() as transaction: print('Retrieving KEKs for Project {}'.format(project.external_id)) query = transaction.session.query(models.KEKDatum) query = query.filter_by(project_id=project.id) @@ -104,7 +109,9 @@ class KekRewrap(object): print('Retrieving all available projects') projects = [] - with self.db_session.begin() as transaction: + db_begin_fn = self.db_session.begin_nested if ( + self.db_session.in_transaction()) else self.db_session.begin + with db_begin_fn() as transaction: projects = transaction.session.query(models.Project).all() return projects