def close(self):
        """Close the connection"""

        if self.closed:
            return

        # Make sure no one minds the connection to be closed
        # This will help avoid MemoryError in other threads,
        # they will get sqlite3.ProgrammingError instead
        if not self.personal_lock.acquire(timeout=self.lock_timeout):
            raise LockTimeoutError(self)

        try:
            try:
                if self.in_transaction:
                    self.db_state.active_connection = None
                    self.db_state.transaction_lock.release()
            except sqlite3.ProgrammingError:
                pass

            if self._cursor is not None and not self._cursor.closed:
                self._cursor._cursor.close()
                self._cursor.closed = True

            if self.connection is not None:
                self.connection.close()

            self.closed = True
        finally:
            self.personal_lock.release()