Skip to content

Commit 723af95

Browse files
committed
allow startTransaction to retry
1 parent 901a27d commit 723af95

File tree

8 files changed

+821
-84
lines changed

8 files changed

+821
-84
lines changed

pymongo/asynchronous/client_session.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ def active(self) -> bool:
413413
def starting(self) -> bool:
414414
return self.state == _TxnState.STARTING
415415

416+
def set_in_progress(self):
417+
self.state = _TxnState.IN_PROGRESS
418+
416419
@property
417420
def pinned_conn(self) -> Optional[AsyncConnection]:
418421
if self.active() and self.conn_mgr:
@@ -1064,8 +1067,6 @@ def _apply_to(
10641067
)
10651068

10661069
if self._transaction.state == _TxnState.STARTING:
1067-
# First command begins a new transaction.
1068-
self._transaction.state = _TxnState.IN_PROGRESS
10691070
command["startTransaction"] = True
10701071

10711072
assert self._transaction.opts

pymongo/asynchronous/mongo_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,8 @@ async def run(self) -> T:
27772777
try:
27782778
res = await self._read() if self._is_read else await self._write()
27792779
await self._retry_policy.record_success(self._attempt_number > 0)
2780+
if self._session._starting_transaction:
2781+
self._session._transaction.set_in_progress()
27802782
return res
27812783
except ServerSelectionTimeoutError:
27822784
# The application may think the write was never attempted

pymongo/synchronous/client_session.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ def active(self) -> bool:
411411
def starting(self) -> bool:
412412
return self.state == _TxnState.STARTING
413413

414+
def set_in_progress(self):
415+
self.state = _TxnState.IN_PROGRESS
416+
414417
@property
415418
def pinned_conn(self) -> Optional[Connection]:
416419
if self.active() and self.conn_mgr:
@@ -1060,8 +1063,6 @@ def _apply_to(
10601063
)
10611064

10621065
if self._transaction.state == _TxnState.STARTING:
1063-
# First command begins a new transaction.
1064-
self._transaction.state = _TxnState.IN_PROGRESS
10651066
command["startTransaction"] = True
10661067

10671068
assert self._transaction.opts

pymongo/synchronous/mongo_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,6 +2767,8 @@ def run(self) -> T:
27672767
try:
27682768
res = self._read() if self._is_read else self._write()
27692769
self._retry_policy.record_success(self._attempt_number > 0)
2770+
if self._session._starting_transaction:
2771+
self._session._transaction.set_in_progress()
27702772
return res
27712773
except ServerSelectionTimeoutError:
27722774
# The application may think the write was never attempted

0 commit comments

Comments
 (0)