SQLAlchemy sessions should be handled independently of the lifetimes of
objects such as V4DB and TestSuiteDB (which basically represent sqlalchemy models).
See also the SQLAlchemy documentation:
http://docs.sqlalchemy.org/en/latest/orm/session_basics.html esp. the
"When do I construct a Session, when do I commit it, and when do I close
it?"; "As a general rule, keep the lifecycle of the session
separate and external from functions and objects that access and/or
manipulate database data. This will greatly help with achieving a
predictable and consistent transactional scope."
This requires a bunch of mechanical code changes to make sure the
session object is passed around separately and a session needs to be
created explicitely now rather than getting a long lived session as part
of V4DB.
Advantages:
- We can construct the DB object and the testsuite table descriptions once when LNT starts. This avoids a bunch of database queries and sqlalchemy model building logic running again for each request.
- We can switch session.rollback() to session.close() in an upcoming commit, avoid countless unnecessary database queries happening on rollback().
- Avoids a bunch of obscure caching effects with long lived sessions helping to clean up LNT code (the cleanup already happened in r312059)
- I hope this will help some of the server instabilities we experience (it's hard to pinpoint the cause though, it may be something different).