Esempio n. 1
0
    def rollback(self, session, transaction_id, options=None):
        """
        Rolls back a transaction, releasing any locks it holds. It is a good
        idea to call this for any transaction that includes one or more
        ``Read`` or ``ExecuteSql`` requests and
        ultimately decides not to commit.

        ``Rollback`` returns ``OK`` if it successfully aborts the transaction, the
        transaction was already aborted, or the transaction is not
        found. ``Rollback`` never returns ``ABORTED``.

        Example:
            >>> from google.cloud import spanner_v1
            >>>
            >>> client = spanner_v1.SpannerClient()
            >>>
            >>> session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]')
            >>> transaction_id = b''
            >>>
            >>> client.rollback(session, transaction_id)

        Args:
            session (str): Required. The session in which the transaction to roll back is running.
            transaction_id (bytes): Required. The transaction to roll back.
            options (~google.gax.CallOptions): Overrides the default
                settings for this call, e.g, timeout, retries etc.

        Raises:
            :exc:`google.gax.errors.GaxError` if the RPC is aborted.
            :exc:`ValueError` if the parameters are invalid.
        """
        request = spanner_pb2.RollbackRequest(
            session=session, transaction_id=transaction_id)
        self._rollback(request, options)
    def rollback(self,
                 session,
                 transaction_id,
                 retry=google.api_core.gapic_v1.method.DEFAULT,
                 timeout=google.api_core.gapic_v1.method.DEFAULT,
                 metadata=None):
        """
        Rolls back a transaction, releasing any locks it holds. It is a good
        idea to call this for any transaction that includes one or more
        ``Read`` or ``ExecuteSql`` requests and
        ultimately decides not to commit.

        ``Rollback`` returns ``OK`` if it successfully aborts the transaction, the
        transaction was already aborted, or the transaction is not
        found. ``Rollback`` never returns ``ABORTED``.

        Example:
            >>> from google.cloud import spanner_v1
            >>>
            >>> client = spanner_v1.SpannerClient()
            >>>
            >>> session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]')
            >>> transaction_id = b''
            >>>
            >>> client.rollback(session, transaction_id)

        Args:
            session (str): Required. The session in which the transaction to roll back is running.
            transaction_id (bytes): Required. The transaction to roll back.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        request = spanner_pb2.RollbackRequest(
            session=session,
            transaction_id=transaction_id,
        )
        self._rollback(
            request, retry=retry, timeout=timeout, metadata=metadata)
Esempio n. 3
0
    def test_rollback(self):
        channel = ChannelStub()
        client = spanner_v1.SpannerClient(channel=channel)

        # Setup Request
        session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]',
                                      '[SESSION]')
        transaction_id = b'28'

        client.rollback(session, transaction_id)

        assert len(channel.requests) == 1
        expected_request = spanner_pb2.RollbackRequest(
            session=session, transaction_id=transaction_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Esempio n. 4
0
def _transaction(stub):
    """Probe to test BeginTransaction, Commit and Rollback grpc from Spanner stub.

  Args:
    stub: An object of SpannerStub.
  """
    _transaction_tracer = initialize_tracer()
    with _transaction_tracer.span(name='_transaction') as root_span:
        root_span.add_annotation('endpoint info available',
                                 endpoint=_SPANNER_TARGET)
        session = None
        try:
            with _transaction_tracer.span(name='stub.CreateSession'):
                session = stub.CreateSession(
                    spanner_pb2.CreateSessionRequest(database=_DATABASE))

            txn_options = transaction_pb2.TransactionOptions(
                read_write=transaction_pb2.TransactionOptions.ReadWrite())
            txn_request = spanner_pb2.BeginTransactionRequest(
                session=session.name,
                options=txn_options,
            )

            # Probing BeginTransaction call
            with _transaction_tracer.span(name='stub.BeginTransaction'):
                txn = stub.BeginTransaction(txn_request)

            # Probing Commit call
            commit_request = spanner_pb2.CommitRequest(session=session.name,
                                                       transaction_id=txn.id)
            with _transaction_tracer.span(name='stub.Commit'):
                stub.Commit(commit_request)

            # Probing Rollback call
            txn = stub.BeginTransaction(txn_request)
            rollback_request = spanner_pb2.RollbackRequest(
                session=session.name, transaction_id=txn.id)
            with _transaction_tracer.span(name='stub.Rollback'):
                stub.Rollback(rollback_request)

        finally:
            if session is not None:
                with _transaction_tracer.span(name='stub.DeleteSession'):
                    stub.DeleteSession(
                        spanner_pb2.DeleteSessionRequest(name=session.name))
Esempio n. 5
0
    def test_rollback(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_v1.SpannerClient()

        # Setup Request
        session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]",
                                      "[SESSION]")
        transaction_id = b"28"

        client.rollback(session, transaction_id)

        assert len(channel.requests) == 1
        expected_request = spanner_pb2.RollbackRequest(
            session=session, transaction_id=transaction_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Esempio n. 6
0
    def test_rollback(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = spanner_v1.SpannerClient()

        # Mock request
        session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]',
                                      '[SESSION]')
        transaction_id = b'28'

        client.rollback(session, transaction_id)

        grpc_stub.Rollback.assert_called_once()
        args, kwargs = grpc_stub.Rollback.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = spanner_pb2.RollbackRequest(
            session=session, transaction_id=transaction_id)
        self.assertEqual(expected_request, actual_request)