File tree Expand file tree Collapse file tree
packages/google-cloud-bigquery
google/cloud/bigquery/dbapi Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -78,10 +78,14 @@ def close(self):
7878 """
7979 self ._closed = True
8080
81- if self ._owns_client :
81+ for cursor_ in list (self ._cursors_created ):
82+ if not cursor_ ._closed :
83+ cursor_ .close ()
84+
85+ if self ._owns_client and self ._client is not None :
8286 self ._client .close ()
8387
84- if self ._owns_bqstorage_client :
88+ if self ._owns_bqstorage_client and self . _bqstorage_client is not None :
8589 # There is no close() on the BQ Storage client itself.
8690 transport = self ._bqstorage_client .transport
8791 transport .close ()
@@ -95,10 +99,6 @@ def close(self):
9599 if channel is not None :
96100 channel .close ()
97101
98- for cursor_ in self ._cursors_created :
99- if not cursor_ ._closed :
100- cursor_ .close ()
101-
102102 def commit (self ):
103103 """No-op, but for consistency raise an error if connection is closed."""
104104
Original file line number Diff line number Diff line change @@ -204,12 +204,16 @@ def _still_in_use(bad_request):
204204 tag_key = key_values .pop ()
205205
206206 # Delete tag values first
207- [
208- tag_values_client .delete_tag_value (name = tag_value .name ).result ()
209- for tag_value in key_values
210- ]
207+ for tag_value in key_values :
208+ try :
209+ tag_values_client .delete_tag_value (name = tag_value .name ).result ()
210+ except NotFound :
211+ pass
211212
212- tag_keys_client .delete_tag_key (name = tag_key .name ).result ()
213+ try :
214+ tag_keys_client .delete_tag_key (name = tag_key .name ).result ()
215+ except NotFound :
216+ pass
213217
214218 def test_get_service_account_email (self ):
215219 client = Config .CLIENT
@@ -2203,11 +2207,15 @@ def test_dbapi_connection_does_not_leak_sockets(self):
22032207 rows = cursor .fetchall ()
22042208 self .assertEqual (len (rows ), 100000 )
22052209
2210+ cursor .close ()
22062211 connection .close ()
2212+
2213+ del connection , cursor , rows
22072214 import gc
22082215
22092216 gc .collect ()
22102217 for _ in range (60 ): # Wait up to 6 seconds for background socket cleanup
2218+ gc .collect ()
22112219 conn_end = current_process .net_connections ()
22122220 conn_count_end = len (conn_end )
22132221 new_conns_remaining = [
You can’t perform that action at this time.
0 commit comments