Exemple #1
0
    def test_connection(self):
        """ Test the socket, if it's errored or closed out, try to reconnect. Otherwise raise and Exception """
        readable, writeable, in_error = self._select([self._conn],
                                                     [self._conn], [], 1)
        if not readable and not writeable:
            for timeout in [2, 4, 8]:
                try:
                    self._conn.shutdown(SHUT_RDWR)
                    self._conn.close()
                    self._conn.connect((self.host, self.port))
                    readable, writeable, _ = self._select([self._conn],
                                                          [self._conn], [],
                                                          timeout)
                    if not readable and not writeable:
                        pass
                    else:
                        # We have reconnected
                        self._conn.settimeout(self.timeout)
                        # indicates that we're in a transaction
                        self._in_transaction = False

                        # set the session key to the pool default
                        if self.pool_session:
                            self._session_key = self.pool_session
                        elif self.session_less is False:
                            self._session_key = None
                            self._open_session()
                        return None
                except Exception as e:
                    # Ignore this at let the outer handler handle iterations
                    pass

            raise RexProConnectionException(
                "Could not reconnect to database %s:%s" %
                (self.host, self.port))
Exemple #2
0
    def connection(self, transaction=True, *args, **kwargs):
        """ Context manager that conveniently grabs a connection from the pool and provides it with the context
        cleanly closes up the connection and restores it to the pool afterwards

        :param host: the rexpro server to connect to
        :type host: str (ip address)
        :param port: the rexpro server port to connect to
        :type port: int
        :param graph_name: the graph to connect to
        :type graph_name: str
        :param graph_obj_name: The graph object to use
        :type graph_obj_name: str
        :param username: the username to use for authentication (optional)
        :type username: str
        :param password: the password to use for authentication (optional)
        :type password: str
        """
        conn = self.create_connection(*args, **kwargs)
        if not conn:
            raise RexProConnectionException(
                "Cannot commit because connection was closed: %r" % (conn, ))

        try:
            if transaction:
                with conn.transaction():
                    yield conn
            else:
                yield conn
        finally:
            self.close_connection(conn, soft=True)
Exemple #3
0
 def close(self):
     self._conn.send_message(
         messages.SessionRequest(
             session_key=self._session_key,
             graph_name=self.graph_name,
             kill_session=True
         )
     )
     response = self._conn.get_response()
     if isinstance(response, ErrorResponse):
         raise RexProConnectionException(response.message)
Exemple #4
0
    def _open_session(self):
        """ Creates a session with rexster and creates the graph object """
        self._conn.send_message(
            messages.SessionRequest(
                username=self.username,
                password=self.password,
                graph_name=self.graph_name
            )
        )
        response = self._conn.get_response()
        if isinstance(response, ErrorResponse):
            raise RexProConnectionException(response.message)
        self._session_key = response.session_key

        self.graph_features = self.execute('g.getFeatures().toMap()')
Exemple #5
0
    def open(self, soft=False):
        """ open the connection to the database

        :param soft: Attempt to re-use the connection, if False (default), create a new socket
        :type soft: bool
        """
        if not soft or not self._opened:
            # connect to server
            self._conn = self.SOCKET_CLASS()
            self._conn.settimeout(self.timeout)
            try:
                self._conn.connect((self.host, self.port))
            except Exception as e:
                raise RexProConnectionException("Could not connect to database: %s" % e)

        # indicate that we're not yet in a transaction
        self._in_transaction = False

        # get a new session key if there isn't one already
        self._opened = True
        if not self._session_key:
            self._open_session()