From e072d06ce649765b9c7f8be13af09cea0ef60a0b Mon Sep 17 00:00:00 2001 From: AKamyshnikova Date: Mon, 25 Jan 2016 13:23:56 +0300 Subject: [PATCH] Improve autonested_transaction Currently in method autonested_transaction from db.api is decided begin(nested) or begin(subtransactions) should be used based on catching the the exception. Instead of this it can be checking is there active session or not. Change-Id: I2b5951bcffef99323e3d68034ae9a3fd9fa1970c --- neutron/db/api.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/neutron/db/api.py b/neutron/db/api.py index 7f65e6bd1ca..90bc94c53e5 100644 --- a/neutron/db/api.py +++ b/neutron/db/api.py @@ -21,7 +21,6 @@ from oslo_db import exception as db_exc from oslo_db.sqlalchemy import session from oslo_utils import excutils from oslo_utils import uuidutils -from sqlalchemy import exc from neutron.common import exceptions as n_exc from neutron.db import common_db_mixin @@ -81,13 +80,12 @@ def get_session(autocommit=True, expire_on_commit=False, use_slave=False): @contextlib.contextmanager def autonested_transaction(sess): """This is a convenience method to not bother with 'nested' parameter.""" - try: - session_context = sess.begin_nested() - except exc.InvalidRequestError: + if sess.is_active: + session_context = sess.begin(nested=True) + else: session_context = sess.begin(subtransactions=True) - finally: - with session_context as tx: - yield tx + with session_context as tx: + yield tx # Common database operation implementations