From: Eugene Nikanorov Date: Thu, 19 Mar 2015 00:59:48 +0000 (+0400) Subject: Use convenience method from db api to create nested transaction X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=a52ce62845c899407879e8afbac611fa78eac769;p=openstack-build%2Fneutron-build.git Use convenience method from db api to create nested transaction Instead of dealing with conditional nesting, use method that creates nested transaction if possible. Change-Id: Icb1fbd5d35dcbecce54426b9ef1e1be18b706d8b --- diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index 74dabca7f..2fc10657d 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -1772,9 +1772,11 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2, return self._create_bulk('port', context, ports) def _create_port_with_mac(self, context, network_id, port_data, - mac_address, nested=False): + mac_address): try: - with context.session.begin(subtransactions=True, nested=nested): + # since this method could either be used within or outside the + # transaction, use convenience method to avoid passing a flag + with db_api.autonested_transaction(context.session): db_port = models_v2.Port(mac_address=mac_address, **port_data) context.session.add(db_port) return db_port @@ -1786,12 +1788,8 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2, for i in range(max_retries): mac = self._generate_mac() try: - # nested = True frames an operation that may potentially fail - # within a transaction, so that it can be rolled back to the - # point before its failure while maintaining the enclosing - # transaction return self._create_port_with_mac( - context, network_id, port_data, mac, nested=True) + context, network_id, port_data, mac) except n_exc.MacAddressInUse: LOG.debug('Generated mac %(mac_address)s exists on ' 'network %(network_id)s',