Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Connection.close() doesn't close cursors #461

@IlyaFaer

Description

@IlyaFaer

According to PEP-249, on connection closing all the related cursors must become no-op as well. The current implementation of closes and checks has an issue at this point.

Connection.close() only sets the connection attribute to True:

def close(self):
self.rollback()
self.__dbhandle = None
self._closed = True

While the Cursor checking method is only looking for the Cursor._closed attribute:

def _raise_if_already_closed(self):
"""
Raise an exception if attempting to use an already closed connection.
"""
if self._closed:
raise InterfaceError('cursor already closed')

That means cursor don't know if the related connection is already closed. Technically, it doesn't look like a big problem, as Cursor.execute() delegates to Connection, and the connection will raise an exception already closed. But there are several commands, which will be runned on a closed Cursor before the exception raised, for example, classify_stmt(sql).

To avoid that it'll be good to add into Cursor._raise_if_already_closed() a line like:

if self._connection.is_closed:
    raise ...

Checking a bool is much more faster than regexping SQL code and passing it into a connection method.

Plus to this, I'd make is_closed attribute public, so that users could check if a cursor/connection is closed.

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the googleapis/python-spanner-django API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions