Rework database session handling
The database session scoping was still an issue, since the currently used contexts are too narrow (causing complications with sqlalchemy's lazy evaluation).
This proposal switches to the request-based database session handling provided by flask-sqlachemy. A new session is created (and closed) for every incoming request.
This should be the proper solution.
But the threading used in the engine code will still cause problems, even with this reduced longevity of database sessions. In the long term we should consider one of the following options:
- A) abandon the usage of threads (e.g. switching to
async
or multi-processing instead) - B) provide an interface for using thread-local database sessions (e.g. spawn a new database session in new threads and extend the
DB.session
interface accordingly)
What do you think?
btw: these changes revert the recently introduced lazy='subquery'
parameter in some relationship definitions. I am not sure, whether these were related to the recently introduced context-based database sessions (see 70ffd1a0).