From 681cb8f366d8253360805ba58321871bfe3fc959 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 7 Jun 2013 15:42:49 +0100 Subject: [PATCH] Switch to teardown_appcontext for docs in sqlalchemy pattern --- docs/patterns/sqlalchemy.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/patterns/sqlalchemy.rst b/docs/patterns/sqlalchemy.rst index c6200187..07a762d8 100644 --- a/docs/patterns/sqlalchemy.rst +++ b/docs/patterns/sqlalchemy.rst @@ -61,11 +61,12 @@ already with the :class:`~sqlalchemy.orm.scoped_session`. To use SQLAlchemy in a declarative way with your application, you just have to put the following code into your application module. Flask will -automatically remove database sessions at the end of the request for you:: +automatically remove database sessions at the end of the request or +when the application shuts down:: from yourapplication.database import db_session - @app.teardown_request + @app.teardown_appcontext def shutdown_session(exception=None): db_session.remove() @@ -135,11 +136,12 @@ Here is an example `database.py` module for your application:: metadata.create_all(bind=engine) As for the declarative approach you need to close the session after -each request. Put this into your application module:: +each request or application context shutdown. Put this into your +application module:: from yourapplication.database import db_session - @app.teardown_request + @app.teardown_appcontext def shutdown_session(exception=None): db_session.remove()