Skip to content

Commit

Permalink
Fix #194. Remove closed cursors from connnection
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Mar 17, 2021
1 parent 7e34e54 commit b3913e0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clickhouse_driver/dbapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ def cursor(self):
self._hosts = client.connection.hosts
else:
client.connection.hosts = self._hosts
cursor = Cursor(client)
cursor = Cursor(client, self)
self.cursors.append(cursor)
return cursor
9 changes: 8 additions & 1 deletion clickhouse_driver/dbapi/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class States(object):

_states = States()

def __init__(self, client):
def __init__(self, client, connection):
self._client = client
self._connection = connection
self._reset_state()

self.arraysize = 1
Expand Down Expand Up @@ -86,6 +87,12 @@ def close(self):
self._client.disconnect()
self._state = self._states.CURSOR_CLOSED

try:
# cursor can be already closed
self._connection.cursors.remove(self)
except ValueError:
pass

def execute(self, operation, parameters=None):
"""
Prepare and execute a database operation (query or command).
Expand Down
8 changes: 8 additions & 0 deletions tests/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ def side_getaddrinfo(host, *args, **kwargs):
# once with host == 'wrong_host'.
self.assertEqual(self.n_calls, 1)

def test_remove_cursor_from_connection_on_closing(self):
with self.created_connection() as connection:
self.assertEqual(len(connection.cursors), 0)
cur = connection.cursor()
self.assertEqual(len(connection.cursors), 1)
cur.close()
self.assertEqual(len(connection.cursors), 0)


class StreamingTestCase(DBAPITestCaseBase):
def test_fetchone(self):
Expand Down

0 comments on commit b3913e0

Please sign in to comment.