Skip to content

Commit cc3ec14

Browse files
author
Jesse
authored
[ES-402013] Close cursors before closing connection (#38)
* Add test: cursors are closed when connection closes Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent a49072e commit cc3ec14

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/databricks/sql/client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,11 @@ def close(self) -> None:
181181
self._close()
182182

183183
def _close(self, close_cursors=True) -> None:
184-
self.thrift_backend.close_session(self._session_handle)
185-
self.open = False
186-
187184
if close_cursors:
188185
for cursor in self._cursors:
189186
cursor.close()
187+
self.thrift_backend.close_session(self._session_handle)
188+
self.open = False
190189

191190
def commit(self):
192191
"""No-op because Databricks does not support transactions"""

tests/e2e/driver_tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,32 @@ def test_decimal_not_returned_as_strings_arrow(self):
544544
decimal_type = arrow_df.field(0).type
545545
self.assertTrue(pyarrow.types.is_decimal(decimal_type))
546546

547+
def test_close_connection_closes_cursors(self):
548+
549+
from databricks.sql.thrift_api.TCLIService import ttypes
550+
551+
with self.connection() as conn:
552+
cursor = conn.cursor()
553+
cursor.execute('SELECT id, id `id2`, id `id3` FROM RANGE(1000000) order by RANDOM()')
554+
ars = cursor.active_result_set
555+
556+
# We must manually run this check because thrift_backend always forces `has_been_closed_server_side` to True
557+
558+
# Cursor op state should be open before connection is closed
559+
status_request = ttypes.TGetOperationStatusReq(operationHandle=ars.command_id, getProgressUpdate=False)
560+
op_status_at_server = ars.thrift_backend._client.GetOperationStatus(status_request)
561+
assert op_status_at_server.operationState != ttypes.TOperationState.CLOSED_STATE
562+
563+
conn.close()
564+
565+
# When connection closes, any cursor operations should no longer exist at the server
566+
with self.assertRaises(thrift.Thrift.TApplicationException) as cm:
567+
op_status_at_server = ars.thrift_backend._client.GetOperationStatus(status_request)
568+
if hasattr(cm, "exception"):
569+
assert "RESOURCE_DOES_NOT_EXIST" in cm.exception.message
570+
571+
572+
547573

548574
# use a RetrySuite to encapsulate these tests which we'll typically want to run together; however keep
549575
# the 429/503 subsuites separate since they execute under different circumstances.

0 commit comments

Comments
 (0)