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
This commit is contained in:
AKamyshnikova 2016-01-25 13:23:56 +03:00 committed by Ann Kamyshnikova
parent d56b5a6718
commit e072d06ce6
1 changed files with 5 additions and 7 deletions

View File

@ -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