|
13 | 13 | RetrySettings, |
14 | 14 | retry_operation_async, |
15 | 15 | ) |
| 16 | +from ...query.base import BaseQueryTxMode |
16 | 17 | from ...query.base import QueryClientSettings |
17 | 18 | from ... import convert |
18 | 19 | from ..._grpc.grpcwrapper import common_utils |
| 20 | +from ..._grpc.grpcwrapper import ydb_query_public_types as _ydb_query_public |
19 | 21 |
|
20 | 22 | logger = logging.getLogger(__name__) |
21 | 23 |
|
@@ -122,6 +124,39 @@ async def wrapped_callee(): |
122 | 124 |
|
123 | 125 | return await retry_operation_async(wrapped_callee, retry_settings) |
124 | 126 |
|
| 127 | + async def retry_tx_async( |
| 128 | + self, |
| 129 | + callee: Callable, |
| 130 | + tx_mode: Optional[BaseQueryTxMode] = None, |
| 131 | + retry_settings: Optional[RetrySettings] = None, |
| 132 | + *args, |
| 133 | + **kwargs, |
| 134 | + ): |
| 135 | + """Special interface to execute a bunch of commands with transaction in a safe, retriable way. |
| 136 | +
|
| 137 | + :param callee: A function, that works with session. |
| 138 | + :param tx_mode: Transaction mode, which is a one from the following choises: |
| 139 | + 1) QuerySerializableReadWrite() which is default mode; |
| 140 | + 2) QueryOnlineReadOnly(allow_inconsistent_reads=False); |
| 141 | + 3) QuerySnapshotReadOnly(); |
| 142 | + 4) QueryStaleReadOnly(). |
| 143 | + :param retry_settings: RetrySettings object. |
| 144 | +
|
| 145 | + :return: Result sets or exception in case of execution errors. |
| 146 | + """ |
| 147 | + |
| 148 | + tx_mode = tx_mode if tx_mode else _ydb_query_public.QuerySerializableReadWrite() |
| 149 | + retry_settings = RetrySettings() if retry_settings is None else retry_settings |
| 150 | + |
| 151 | + async def wrapped_callee(): |
| 152 | + async with self.checkout() as session: |
| 153 | + async with session.transaction(tx_mode=tx_mode) as tx: |
| 154 | + result = await callee(tx, *args, **kwargs) |
| 155 | + await tx.commit() |
| 156 | + return result |
| 157 | + |
| 158 | + return await retry_operation_async(wrapped_callee, retry_settings) |
| 159 | + |
125 | 160 | async def execute_with_retries( |
126 | 161 | self, |
127 | 162 | query: str, |
|
0 commit comments