Thank you, the event worked like a charm :-) Though I think that I don't need the commit events, because the application terminates anyway.
I modified your approach to gather which objects were flushed so that in the end I can give the user more precise information:
def update_session_info(session):
dirty |= set(session.dirty)
deleted |= set(session.deleted)
return new, dirty, deleted
@event.listens_for(dbsession, "before_flush")
def on_before_flush(session, _, _):
update_session_info(session)
...
code.interact(local=locals())
...
new, dirty, deleted = update_session_info(dbsession)
if new or dirty or deleted:
if new:
print("The following objects were created: ", new)
if dirty:
print("The following objects were modified: ", dirty)
if deleted:
print("The following objects were deleted: ", deleted)
yesno = input("Would you like to commit this transaction? [y/N] ")
if yesno == "y":
print("Committing transaction...")
else:
print("Rolling back transaction...")
raise _SessionRollbackException()
# ...this is where the context closes and the transaction commits and the dbsession ends.