2

在 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()
...
4

1 回答 1

3

鱼:

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

钓鱼竿

于 2013-03-18T22:13:38.270 回答