@@ -708,20 +708,30 @@ class B(Document):
708708 B .drop_collection ()
709709 b_doc = B .objects .create (name = "b" )
710710
711- with pytest .raises (TestRollbackError ):
712- with run_in_transaction ():
713- a_doc .update (name = "trx-parent" )
714- try :
715- with run_in_transaction ():
716- b_doc .update (name = "trx-child" )
717- raise TestRollbackError ()
718- except TestRollbackError as exc :
719- # at this stage, the parent transaction is still there
720- assert A .objects .get (id = a_doc .id ).name == "trx-parent"
721- raise exc
711+ def run_tx ():
712+ try :
713+ with run_in_transaction ():
714+ a_doc .update (name = "trx-parent" )
715+ try :
716+ with run_in_transaction ():
717+ b_doc .update (name = "trx-child" )
718+ raise TestRollbackError ()
719+ except TestRollbackError as exc :
720+ # at this stage, the parent transaction is still there
721+ assert A .objects .get (id = a_doc .id ).name == "trx-parent"
722+ raise exc
723+ except OperationError as op_failure :
724+ """
725+ See thread safety test below for more details about TransientTransactionError handling
726+ """
727+ if "TransientTransactionError" in str (op_failure ):
728+ logging .warning ("TransientTransactionError - retrying..." )
729+ run_tx ()
722730 else :
723- # makes sure it enters the except above
724- assert False
731+ raise op_failure
732+
733+ with pytest .raises (TestRollbackError ):
734+ run_tx ()
725735
726736 assert A .objects .get (id = a_doc .id ).name == "a"
727737 assert B .objects .get (id = b_doc .id ).name == "b"
0 commit comments