|
53 | 53 | _metadata_with_prefix, |
54 | 54 | _metadata_with_leader_aware_routing, |
55 | 55 | ) |
56 | | -from google.cloud.spanner_v1._opentelemetry_tracing import ( |
57 | | - get_current_span, |
58 | | - trace_call, |
59 | | -) |
60 | 56 | from google.cloud.spanner_v1.batch import Batch |
61 | 57 | from google.cloud.spanner_v1.batch import MutationGroups |
62 | 58 | from google.cloud.spanner_v1.keyset import KeySet |
|
71 | 67 | SpannerGrpcTransport, |
72 | 68 | ) |
73 | 69 | from google.cloud.spanner_v1.table import Table |
| 70 | +from google.cloud.spanner_v1._opentelemetry_tracing import get_current_span |
74 | 71 |
|
75 | 72 |
|
76 | 73 | SPANNER_DATA_SCOPE = "https://www.googleapis.com/auth/spanner.data" |
@@ -698,7 +695,7 @@ def execute_partitioned_dml( |
698 | 695 | ) |
699 | 696 |
|
700 | 697 | def execute_pdml(): |
701 | | - def do_execute_pdml(session): |
| 698 | + with SessionCheckout(self._pool) as session: |
702 | 699 | txn = api.begin_transaction( |
703 | 700 | session=session.name, options=txn_options, metadata=metadata |
704 | 701 | ) |
@@ -730,15 +727,6 @@ def do_execute_pdml(session): |
730 | 727 |
|
731 | 728 | return result_set.stats.row_count_lower_bound |
732 | 729 |
|
733 | | - with SessionCheckout(self._pool) as session: |
734 | | - observability_options = getattr(self, "observability_options", None) |
735 | | - with trace_call( |
736 | | - "CloudSpanner.execute_pdml", |
737 | | - session, |
738 | | - observability_options=observability_options, |
739 | | - ): |
740 | | - return do_execute_pdml(session) |
741 | | - |
742 | 730 | return _retry_on_aborted(execute_pdml, DEFAULT_RETRY_BACKOFF)() |
743 | 731 |
|
744 | 732 | def session(self, labels=None, database_role=None): |
@@ -893,26 +881,17 @@ def run_in_transaction(self, func, *args, **kw): |
893 | 881 | # Sanity check: Is there a transaction already running? |
894 | 882 | # If there is, then raise a red flag. Otherwise, mark that this one |
895 | 883 | # is running. |
896 | | - with SessionCheckout(self._pool) as session: |
897 | | - observability_options = getattr(self, "observability_options", None) |
898 | | - with trace_call( |
899 | | - "CloudSpanner.Database.run_in_transaction", |
900 | | - session, |
901 | | - observability_options=observability_options, |
902 | | - ): |
903 | | - # Sanity check: Is there a transaction already running? |
904 | | - # If there is, then raise a red flag. Otherwise, mark that this one |
905 | | - # is running. |
906 | | - if getattr(self._local, "transaction_running", False): |
907 | | - raise RuntimeError("Spanner does not support nested transactions.") |
908 | | - self._local.transaction_running = True |
909 | | - |
910 | | - # Check out a session and run the function in a transaction; once |
911 | | - # done, flip the sanity check bit back. |
912 | | - try: |
913 | | - return session.run_in_transaction(func, *args, **kw) |
914 | | - finally: |
915 | | - self._local.transaction_running = False |
| 884 | + if getattr(self._local, "transaction_running", False): |
| 885 | + raise RuntimeError("Spanner does not support nested transactions.") |
| 886 | + self._local.transaction_running = True |
| 887 | + |
| 888 | + # Check out a session and run the function in a transaction; once |
| 889 | + # done, flip the sanity check bit back. |
| 890 | + try: |
| 891 | + with SessionCheckout(self._pool) as session: |
| 892 | + return session.run_in_transaction(func, *args, **kw) |
| 893 | + finally: |
| 894 | + self._local.transaction_running = False |
916 | 895 |
|
917 | 896 | def restore(self, source): |
918 | 897 | """Restore from a backup to this database. |
@@ -1189,7 +1168,7 @@ def __enter__(self): |
1189 | 1168 | current_span = get_current_span() |
1190 | 1169 | session = self._session = self._database._pool.get() |
1191 | 1170 | if current_span: |
1192 | | - current_span.add_event("Using session", {"id": self._session.session_id}) |
| 1171 | + current_span.add_event("Using session", {"id": session.session_id}) |
1193 | 1172 | batch = self._batch = Batch(session) |
1194 | 1173 | if self._request_options.transaction_tag: |
1195 | 1174 | batch.transaction_tag = self._request_options.transaction_tag |
|
0 commit comments