GeezerVison is showing github.com — fix: transaction_tag should be set on BeginTransactionRequest (#1463) · googleapis/python-spanner@3d3cea0
This repository was archived by the owner on Jun 8, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/snapshot.py +19-4Lines changed: 19 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -901,13 +901,19 @@ def attempt_tracking_method(): | 901 | 901 |
| 902 | 902 | return [partition.partition_token for partition in response.partitions] | 903 | 903 |
| 904 | | - def _begin_transaction(self, mutation: Mutation = None) -> bytes: | | 904 | + def _begin_transaction( | | 905 | + self, mutation: Mutation = None, transaction_tag: str = None | | 906 | + ) -> bytes: | 905 | 907 | """Begins a transaction on the database. | 906 | 908 |
| 907 | 909 | :type mutation: :class:`~google.cloud.spanner_v1.mutation.Mutation` | 908 | 910 | :param mutation: (Optional) Mutation to include in the begin transaction | 909 | 911 | request. Required for mutation-only transactions with multiplexed sessions. | 910 | 912 |
| | 913 | + :type transaction_tag: str | | 914 | + :param transaction_tag: (Optional) Transaction tag to include in the begin transaction | | 915 | + request. | | 916 | +
| 911 | 917 | :rtype: bytes | 912 | 918 | :returns: identifier for the transaction. | 913 | 919 |
| @@ -931,6 +937,17 @@ def _begin_transaction(self, mutation: Mutation = None) -> bytes: | 931 | 937 | (_metadata_with_leader_aware_routing(database._route_to_leader_enabled)) | 932 | 938 | ) | 933 | 939 |
| | 940 | + begin_request_kwargs = { | | 941 | + "session": session.name, | | 942 | + "options": self._build_transaction_selector_pb().begin, | | 943 | + "mutation_key": mutation, | | 944 | + } | | 945 | + | | 946 | + if transaction_tag: | | 947 | + begin_request_kwargs["request_options"] = RequestOptions( | | 948 | + transaction_tag=transaction_tag | | 949 | + ) | | 950 | + | 934 | 951 | with trace_call( | 935 | 952 | name=f"CloudSpanner.{type(self).__name__}.begin", | 936 | 953 | session=session, | @@ -942,9 +959,7 @@ def _begin_transaction(self, mutation: Mutation = None) -> bytes: | 942 | 959 |
| 943 | 960 | def wrapped_method(): | 944 | 961 | begin_transaction_request = BeginTransactionRequest( | 945 | | - session=session.name, | 946 | | - options=self._build_transaction_selector_pb().begin, | 947 | | - mutation_key=mutation, | | 962 | + **begin_request_kwargs | 948 | 963 | ) | 949 | 964 | begin_transaction_method = functools.partial( | 950 | 965 | api.begin_transaction, | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/transaction.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -714,7 +714,9 @@ def _begin_transaction(self, mutation: Mutation = None) -> bytes: | 714 | 714 | if self.rolled_back: | 715 | 715 | raise ValueError("Transaction is already rolled back") | 716 | 716 |
| 717 | | - return super(Transaction, self)._begin_transaction(mutation=mutation) | | 717 | + return super(Transaction, self)._begin_transaction( | | 718 | + mutation=mutation, transaction_tag=self.transaction_tag | | 719 | + ) | 718 | 720 |
| 719 | 721 | def _begin_mutations_only_transaction(self) -> None: | 720 | 722 | """Begins a mutations-only transaction on the database.""" | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_session.py +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2005,9 +2005,12 @@ def unit_of_work(txn, *args, **kw): | 2005 | 2005 | self.assertEqual(kw, {"some_arg": "def"}) | 2006 | 2006 |
| 2007 | 2007 | expected_options = TransactionOptions(read_write=TransactionOptions.ReadWrite()) | | 2008 | + expected_request_options = RequestOptions(transaction_tag=transaction_tag) | 2008 | 2009 | gax_api.begin_transaction.assert_called_once_with( | 2009 | 2010 | request=BeginTransactionRequest( | 2010 | | - session=self.SESSION_NAME, options=expected_options | | 2011 | + session=self.SESSION_NAME, | | 2012 | + options=expected_options, | | 2013 | + request_options=expected_request_options, | 2011 | 2014 | ), | 2012 | 2015 | metadata=[ | 2013 | 2016 | ("google-cloud-resource-prefix", database.name), | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_transaction.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -467,6 +467,7 @@ def _commit_helper( | 467 | 467 | session=session.name, | 468 | 468 | options=TransactionOptions(read_write=TransactionOptions.ReadWrite()), | 469 | 469 | mutation_key=expected_begin_mutation, | | 470 | + request_options=RequestOptions(transaction_tag=TRANSACTION_TAG), | 470 | 471 | ) | 471 | 472 |
| 472 | 473 | expected_begin_metadata = base_metadata.copy() | |
|