Don't use ModelBase.save() inside of block session.begin()

Bug #1224429 reported by ChangBo Guo(gcb)
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Cinder
Fix Released
Undecided
ChangBo Guo(gcb)
OpenStack Compute (nova)
Fix Released
Undecided
ChangBo Guo(gcb)
Sahara
Fix Released
Medium
ChangBo Guo(gcb)

Bug Description

Current code use ModelBase.save() always submit a commit even inside a block fo with session.begin().
this is not purpose of using session.begin() to organize some operations in one transaction

1)session.begin() will return a SessionTransaction instance, then call SessionTransaction.__enter__()
and do something in with block, then call SessionTransaction.__exit__(), in method __exit__() will commit or rollback automatically. See https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/session.py#L454

2) There is also suggestion metioned in https://github.com/openstack/oslo-incubator/blob/master/openstack/common/db/sqlalchemy/session.py#L71

3) ModelBase.save() begin another transaction see https://github.com/openstack/oslo-incubator/blob/master/openstack/common/db/sqlalchemy/models.py#L47

so we'd better don't use ModelBase.save() inside of block session.begin()

Tags: 0.3
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to nova (master)

Fix proposed to branch: master
Review: https://review.openstack.org/46253

Changed in nova:
assignee: nobody → ChangBo Guo (guochbo)
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to savanna (master)

Reviewed: https://review.openstack.org/46258
Committed: http://github.com/stackforge/savanna/commit/9390ce7b035a7d60d0c665dc0891b67f7436e8f3
Submitter: Jenkins
Branch: master

commit 9390ce7b035a7d60d0c665dc0891b67f7436e8f3
Author: Chang Bo Guo <email address hidden>
Date: Thu Sep 12 04:45:31 2013 -0700

    Don't use ModelBase.save() inside of transaction

    We using block 'with session.begin()' to organize some operations in
    one transaction. session.begin() returns a transaction instance, then
    does some operations, and will commit or rollback automatically
    before leaving the block. ModelBase.save() always submit a commit,
    and that is not expected.
    When we get a persistent object from database, we just modify the
    object inside of block 'with session.begin()' and sqlalchemy will
    update it,don't need method session.add() or ModelBase.save()

    Fixes bug #1224429

    Change-Id: I2204b3023c01e65950e6131cd08b4ded5564e9b9

Changed in savanna:
status: New → Fix Committed
Changed in savanna:
assignee: nobody → ChangBo Guo (guochbo)
milestone: none → 0.3a1
importance: Undecided → Medium
tags: added: 0.3
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to nova (master)

Reviewed: https://review.openstack.org/46253
Committed: http://github.com/openstack/nova/commit/037d1e9b1b89fb0a969b1b02ee4c74550b7864b0
Submitter: Jenkins
Branch: master

commit 037d1e9b1b89fb0a969b1b02ee4c74550b7864b0
Author: Chang Bo Guo <email address hidden>
Date: Thu Sep 12 04:27:40 2013 -0700

    Don't use ModelBase.save() inside of transaction

    We using block 'with session.begin()' to organize some operations in
    one transaction. session.begin() returns a transaction instance, then
    does some operations, and will commit or rollback automatically
    before leaving the block. ModelBase.save() always submit a commit,
    and that is not expected.
    When we get a persistent object from database, we just modify the
    object inside of block 'with session.begin()' and sqlalchemy will
    update it,don't need method session.add() or ModelBase.save()

    Fixes bug #1224429

    Change-Id: I8af3fd4b62a915833bf303b6892d432043fb57a9

Changed in nova:
status: In Progress → Fix Committed
Changed in nova:
milestone: none → havana-rc1
Changed in savanna:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in nova:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in nova:
milestone: havana-rc1 → 2013.2
Changed in savanna:
milestone: 0.3a1 → 0.3
description: updated
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to cinder (master)

Fix proposed to branch: master
Review: https://review.openstack.org/78141

Changed in cinder:
assignee: nobody → ChangBo Guo(gcb) (glongwave)
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to cinder (master)

Reviewed: https://review.openstack.org/78141
Committed: https://git.openstack.org/cgit/openstack/cinder/commit/?id=13387c01390e70c9d7811760e603e6306d6d7ea7
Submitter: Jenkins
Branch: master

commit 13387c01390e70c9d7811760e603e6306d6d7ea7
Author: ChangBo Guo(gcb) <email address hidden>
Date: Wed Mar 5 17:00:58 2014 +0800

    Don't use ModelBase.save() inside of transaction

    'with session.begin()' makes some operations in one transaction.
    session.begin() returns a transaction instance, then does some operations,
    and will commit or rollback automatically before leaving the block.
    ModelBase.save() always submit a commit, and that is not expected.
    When we get a persistent object from database, we just modify the
    object inside of block 'with session.begin()' and sqlalchemy will
    update it, don't need method session.add() or ModelBase.save().

    Closes-Bug: #1224429
    Change-Id: I4af58e98b2783d3945d92e57680d58e7ae356a67

Changed in cinder:
status: In Progress → Fix Committed
Changed in cinder:
milestone: none → juno-2
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in cinder:
milestone: juno-2 → 2014.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.