【问题标题】:SQLAlchemy session after exception异常后的 SQLAlchemy 会话
【发布时间】:2013-03-06 13:02:44
【问题描述】:

在 SQLAlchemy 中,发生异常后,由于回滚,需要再次设置会话。因此,我是否应该始终设置通话之间的会话?

user = User('username', 'Full Name', 'password', 'email',
                datetime.now(), 'username')
session.add(user)
try:
    session.commit()
except SQLAlchemyError:
    pprint('Not quite right...')

# the session needs to be re-instantiated in case of an exception.
# should I always do it or only if there was an exception above?
session = Session()
res = session.query(User).all()
...

【问题讨论】:

    标签: python session exception sqlalchemy


    【解决方案1】:

    鱼:

    try:
        session.commit()
    except SQLAlchemyError:
        pprint('Not quite right...')
        session.rollback()
    

    Fishing rod.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2013-04-09
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多