Avoid DetachedInstanceError after session rollback

In some cases this exception is thrown while accessing Agent
object from logging statement after a transaction was rolled back.
There is a unit test that covers thsi code patch, but the issue
is not reproducible with sqlite.
Just avoid accessing db object after session had been closed.

Change-Id: Iff6b72156b08f177bd0c71f6ba93d3bf46c82fa4
Closes-Bug: #1424578
This commit is contained in:
Eugene Nikanorov 2015-02-23 13:29:08 +03:00
parent 194afde3a6
commit 4d4de151e9
1 changed files with 8 additions and 5 deletions

View File

@ -38,10 +38,13 @@ class ChanceScheduler(object):
def _schedule_bind_network(self, context, agents, network_id):
for agent in agents:
context.session.begin(subtransactions=True)
# saving agent_id to use it after rollback to avoid
# DetachedInstanceError
agent_id = agent.id
binding = agentschedulers_db.NetworkDhcpAgentBinding()
binding.dhcp_agent_id = agent_id
binding.network_id = network_id
try:
binding = agentschedulers_db.NetworkDhcpAgentBinding()
binding.dhcp_agent = agent
binding.network_id = network_id
context.session.add(binding)
# try to actually write the changes and catch integrity
# DBDuplicateEntry
@ -49,11 +52,11 @@ class ChanceScheduler(object):
except db_exc.DBDuplicateEntry:
# it's totally ok, someone just did our job!
context.session.rollback()
LOG.info(_LI('Agent %s already present'), agent)
LOG.info(_LI('Agent %s already present'), agent_id)
LOG.debug('Network %(network_id)s is scheduled to be '
'hosted by DHCP agent %(agent_id)s',
{'network_id': network_id,
'agent_id': agent})
'agent_id': agent_id})
def schedule(self, plugin, context, network):
"""Schedule the network to active DHCP agent(s).