Hi,
Currently working on a project that use SQL alchemy with mod_wsgi, webapp2 and prestans (python REST framework). The backend is Oracle XE using cx_Oracle.
What is the appropriate way to manage sessions in a multithreading app such as this? Currently I have been following the guide found
here to implement a contextual/thread-local session.
I have then made a session that is automatically cleaned up by the request handler. The code for this looks something like:
def dispatch(self):
self.db_session = db.Session()
super(BaseHandler, self).dispatch()
self.db_session.close()
db.Session.remove()
However this doesn't seem to clean up all database connections as viewed in netstat and I get a connections exhausted error from Oracle XE after a certain time of using the application. The same thing also happens when I use MySQL as well.
If I add a call to db.engine.dispose() after the handler has returned this appears to fix the problem but the SQL Alchemy documentation indicates that this is only for usage with dropped connections or in test suites.
What is the best practice for usage of a session in a multithreading application that ensures connections are cleaned up?
Thanks,
Brad