简体   繁体   English

我如何更好地处理Flask-SQLAlchemy提交/回滚?

[英]How can I better handle this Flask-SQLAlchemy commit/rollback?

I'm reviewing some old code I wrote and was looking at a shared commit function I had written to handle responses to the user on certain failures when attempting to commit changes to the database (such as deletes): 我正在查看我编写的一些旧代码,并查看了我编写的共享提交功能,该功能用于在尝试将更改提交给数据库(例如删除)时处理某些失败时对用户的响应:

def _commit_to_database():
    """A shared function to make a commit to the database and handle exceptions
    if encountered.
    """
    flask.current_app.logger.info('Committing changes to database...')
    try:
        db.session.commit()
    except AssertionError as err:
        flask.abort(409, err)
    except (exc.IntegrityError, sqlite3.IntegrityError) as err:
        flask.abort(409, err.orig)
    except Exception as err:
        flask.abort(500, err)
    finally:
        db.session.rollback()

I think I understand my thought process: attempt the commit , upon certain failures trigger a flask.abort to send the response back, but I believe I found that the database was left with an open session requiring a rollback when I did this and adding the rollback into a finally statement resolved this allowing me to still use the flask.abort . 我认为我理解了我的思维过程:尝试执行commit ,在某些失败时触发flask.abort将响应发送回去,但是我发现执行此操作并添加了rollbackfinally语句解决了此问题,使我仍然可以使用flask.abort

The questions I have around me code are: 我对我的代码有以下疑问:

1) Is this a bug: will the Flask-SQLAlchemy extension not close out the session as normal; 1)这是否是一个错误:Flask-SQLAlchemy扩展是否不会正常关闭会话; is calling the rollback on the finally which will be triggering after the abort going to affect successful commit s? abort将影响成功commit的触发后将在finally上调用rollback吗?

2) If this is a bug: what should I be doing differently in handling the try-except-finally and the db session? 2)如果这是一个错误:在处理try-except-finallydb会话时,我应该做些什么?

You need to rollback when exception occurs and finally close the session: 您需要在发生异常时回滚并最终关闭会话:

def _commit_to_database(): 
    """A shared function to make a   
       commit to the database and
       handle exceptions if encountered.
    """
    flask.current_app.logger.info(
        'Committing changes to db...'
    )
    try: 
        db.session.commit() 
    except AssertionError as err:
        db.session.rollback()
        flask.abort(409, err) 
    except (
        exc.IntegrityError, 
        sqlite3.IntegrityError
     ) as err:
        db.session.rollback()
        flask.abort(409, err.orig) 
    except Exception as err:
        db.session.rollback()
        flask.abort(500, err) 
    finally: 
        db.session.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果引发异常,如何使 Flask-SQLAlchemy 自动回滚会话? - How to make Flask-SQLAlchemy automatically rollback the session if an exception is raised? 如何避免在flask-sqlalchemy中的Raw SQL中使用ROLLBACK语句 - How to avoid ROLLBACK statement in Raw SQL in flask-sqlalchemy 如何将flask-sqlalchemy与Google Cloud Functions结合使用? - How can I use flask-sqlalchemy with Google Cloud Functions? 我如何在 Flask-SQLAlchemy 中执行此查询? - How can I do this query in Flask-SQLAlchemy? 如何迁移 flask-sqlalchemy 多个数据库? - How can I migrate flask-sqlalchemy multiple databases? 如何使用flask-sqlalchemy编写doctest? - How can I write doctests with flask-sqlalchemy? 如何使用flask-sqlalchemy查询博客帖子的类别? - How can I query categories for blog posts with flask-sqlalchemy? 如何防止 Flask-SQLAlchemy 中的 SQL 注入? 有没有更好的方法从 CSV 加载数据? - How do I prevent SQL injections in Flask-SQLAlchemy? Is there a better way to load data from CSV? 我可以同时使用Flask-SQLAlchemy和SQLAlchemy吗? - Can I use Flask-SQLAlchemy and SQLAlchemy at the same time? 如何使用 Flask-SQLAlchemy 在 Flask 中使用 Python 将当前会话设置为“登录”? - How can I set the current session as 'logged in' using Python in Flask with Flask-SQLAlchemy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM