File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -505,11 +505,13 @@ def __enter__(self):
505
505
"""Get a connection from the pool.
506
506
"""
507
507
self .conn = self .pool .getconn ()
508
- return self .conn .cursor (* self .a , ** self .kw )
508
+ self .cursor = self .conn .cursor (* self .a , ** self .kw )
509
+ return self .cursor
509
510
510
511
def __exit__ (self , * exc_info ):
511
512
"""Put our connection back in the pool.
512
513
"""
514
+ self .cursor .close ()
513
515
self .pool .putconn (self .conn )
514
516
515
517
@@ -540,7 +542,8 @@ def __enter__(self):
540
542
"""
541
543
self .conn = self .pool .getconn ()
542
544
self .conn .autocommit = False
543
- return self .conn .cursor (* self .a , ** self .kw )
545
+ self .cursor = self .conn .cursor (* self .a , ** self .kw )
546
+ return self .cursor
544
547
545
548
def __exit__ (self , * exc_info ):
546
549
"""Put our connection back in the pool.
@@ -549,6 +552,7 @@ def __exit__(self, *exc_info):
549
552
self .conn .commit ()
550
553
else :
551
554
self .conn .rollback ()
555
+ self .cursor .close ()
552
556
self .conn .autocommit = True
553
557
self .pool .putconn (self .conn )
554
558
Original file line number Diff line number Diff line change 6
6
7
7
from postgres import Postgres , TooFew , TooMany
8
8
from psycopg2 .extras import NamedTupleCursor
9
+ from psycopg2 import InterfaceError
9
10
10
11
11
12
DATABASE_URL = os .environ ['DATABASE_URL' ]
@@ -233,6 +234,13 @@ def test_we_can_use_cursor_closed(self):
233
234
actual = cursor .closed
234
235
assert not actual
235
236
237
+ def test_we_close_the_cursor (self ):
238
+ with self .db .get_cursor () as cursor :
239
+ cursor .execute ("SELECT * FROM foo ORDER BY bar" )
240
+ self .assertRaises ( InterfaceError
241
+ , cursor .fetchall
242
+ )
243
+
236
244
237
245
# db.get_transaction
238
246
# ==================
@@ -272,6 +280,13 @@ class Heck(Exception): pass
272
280
actual = self .db .all ("SELECT * FROM foo ORDER BY bar" )
273
281
assert actual == [{"bar" : "baz" }, {"bar" : "buz" }]
274
282
283
+ def test_we_close_the_cursor (self ):
284
+ with self .db .get_transaction () as txn :
285
+ txn .execute ("SELECT * FROM foo ORDER BY bar" )
286
+ self .assertRaises ( InterfaceError
287
+ , txn .fetchall
288
+ )
289
+
275
290
276
291
# db.get_connection
277
292
# =================
You can’t perform that action at this time.
0 commit comments