Skip to content

Commit 28d781e

Browse files
committed
fix: transaction_tag should be set on BeginTransactionRequest
1 parent 24394d6 commit 28d781e

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

google/cloud/spanner_v1/snapshot.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,13 +901,17 @@ def attempt_tracking_method():
901901

902902
return [partition.partition_token for partition in response.partitions]
903903

904-
def _begin_transaction(self, mutation: Mutation = None) -> bytes:
904+
def _begin_transaction(self, mutation: Mutation = None, transaction_tag: str = None) -> bytes:
905905
"""Begins a transaction on the database.
906906
907907
:type mutation: :class:`~google.cloud.spanner_v1.mutation.Mutation`
908908
:param mutation: (Optional) Mutation to include in the begin transaction
909909
request. Required for mutation-only transactions with multiplexed sessions.
910910
911+
:type transaction_tag: str
912+
:param transaction_tag: (Optional) Transaction tag to include in the begin transaction
913+
request. Required for multiplexed sessions.
914+
911915
:rtype: bytes
912916
:returns: identifier for the transaction.
913917
@@ -931,6 +935,10 @@ def _begin_transaction(self, mutation: Mutation = None) -> bytes:
931935
(_metadata_with_leader_aware_routing(database._route_to_leader_enabled))
932936
)
933937

938+
request_options = RequestOptions()
939+
if transaction_tag is not None:
940+
request_options.transaction_tag = transaction_tag
941+
934942
with trace_call(
935943
name=f"CloudSpanner.{type(self).__name__}.begin",
936944
session=session,
@@ -944,6 +952,7 @@ def wrapped_method():
944952
begin_transaction_request = BeginTransactionRequest(
945953
session=session.name,
946954
options=self._build_transaction_selector_pb().begin,
955+
request_options=request_options,
947956
mutation_key=mutation,
948957
)
949958
begin_transaction_method = functools.partial(

google/cloud/spanner_v1/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def _begin_transaction(self, mutation: Mutation = None) -> bytes:
714714
if self.rolled_back:
715715
raise ValueError("Transaction is already rolled back")
716716

717-
return super(Transaction, self)._begin_transaction(mutation=mutation)
717+
return super(Transaction, self)._begin_transaction(mutation=mutation, transaction_tag=self.transaction_tag)
718718

719719
def _begin_mutations_only_transaction(self) -> None:
720720
"""Begins a mutations-only transaction on the database."""

tests/unit/test_transaction.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,14 @@ def _commit_helper(
463463
if mutations is not None:
464464
self.assertEqual(transaction._transaction_id, TRANSACTION_ID)
465465

466+
request_options = RequestOptions()
467+
request_options.transaction_tag = TRANSACTION_TAG
468+
466469
expected_begin_transaction_request = BeginTransactionRequest(
467470
session=session.name,
468471
options=TransactionOptions(read_write=TransactionOptions.ReadWrite()),
469472
mutation_key=expected_begin_mutation,
473+
request_options=request_options,
470474
)
471475

472476
expected_begin_metadata = base_metadata.copy()

0 commit comments

Comments
 (0)