File tree 5 files changed +24
-5
lines changed
5 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,15 @@ So here is a simple example of how you can use SQLite 3 with Flask::
24
24
25
25
@app.teardown_request
26
26
def teardown_request(exception):
27
- g.db.close()
27
+ if hasattr(g, 'db'):
28
+ g.db.close()
29
+
30
+ .. note ::
31
+
32
+ Please keep in mind that the teardown request functions are always
33
+ executed, even if a before-request handler failed or was never
34
+ executed. Because of this we have to make sure here that the database
35
+ is there before we close it.
28
36
29
37
Connect on Demand
30
38
-----------------
Original file line number Diff line number Diff line change @@ -131,7 +131,9 @@ understand what is actually happening. The new behavior is quite simple:
131
131
132
132
4. At the end of the request the :meth: `~flask.Flask.teardown_request `
133
133
functions are executed. This always happens, even in case of an
134
- unhandled exception down the road.
134
+ unhandled exception down the road or if a before-request handler was
135
+ not executed yet or at all (for example in test environments sometimes
136
+ you might want to not execute before-request callbacks).
135
137
136
138
Now what happens on errors? In production mode if an exception is not
137
139
caught, the 500 internal server handler is called. In development mode
@@ -183,6 +185,12 @@ It's easy to see the behavior from the command line:
183
185
this runs after request
184
186
>>>
185
187
188
+ Keep in mind that teardown callbacks are always executed, even if
189
+ before-request callbacks were not executed yet but an exception happened.
190
+ Certain parts of the test system might also temporarily create a request
191
+ context without calling the before-request handlers. Make sure to write
192
+ your teardown-request handlers in a way that they will never fail.
193
+
186
194
.. _notes-on-proxies :
187
195
188
196
Notes On Proxies
Original file line number Diff line number Diff line change @@ -142,7 +142,8 @@ You are now encouraged to use this instead::
142
142
143
143
@app.teardown_request
144
144
def after_request(exception):
145
- g.db.close()
145
+ if hasattr(g, 'db'):
146
+ g.db.close()
146
147
147
148
On the upside this change greatly improves the internal code flow and
148
149
makes it easier to customize the dispatching and error handling. This
Original file line number Diff line number Diff line change @@ -50,7 +50,8 @@ def before_request():
50
50
@app .teardown_request
51
51
def teardown_request (exception ):
52
52
"""Closes the database again at the end of the request."""
53
- g .db .close ()
53
+ if hasattr (g , 'db' ):
54
+ g .db .close ()
54
55
55
56
56
57
@app .route ('/' )
Original file line number Diff line number Diff line change @@ -85,7 +85,8 @@ def before_request():
85
85
@app .teardown_request
86
86
def teardown_request (exception ):
87
87
"""Closes the database again at the end of the request."""
88
- g .db .close ()
88
+ if hasattr (g , 'db' ):
89
+ g .db .close ()
89
90
90
91
91
92
@app .route ('/' )
You can’t perform that action at this time.
0 commit comments