aiohttp+sqlalchemy: Can't reconnect until invalid transaction is rolled back

This would be a fairly late answer. This is what happens: While using the session, a sqlalchemy Error is raised (anything which would also throw an error when be used as pure SQL: syntax errors, unique constraints, key collisions etc.).

You would have to find this error, wrap it into a try/except-block and perform a session.rollback().

After this you can reinstate your session.


Got here from a flask_sqlalchemy use case. The fix is to run db.session.rollback() just once and it clears. It is similar to the accepted answer of executing rollback