code: def commit_transaction(self):
          
          self._check_ended()
          retry = False
          state = self._transaction.state
          if state is _TxnState.NONE:
              raise InvalidOperation("No transaction started")
          elif state in (_TxnState.STARTING, _TxnState.COMMITTED_EMPTY):
              # Server transaction was never started, no need to send a command.
              self._transaction.state = _TxnState.COMMITTED_EMPTY
              return
          elif state is _TxnState.ABORTED:
              raise InvalidOperation(
                  "Cannot call commitTransaction after calling abortTransaction")
          elif state is _TxnState.COMMITTED:
              # We're explicitly retrying the commit, move the state back to
              # "in progress" so that _in_transaction returns true.
              self._transaction.state = _TxnState.IN_PROGRESS
              retry = True
  
          try:
              self._finish_transaction_with_retry("commitTransaction", retry)
          except ConnectionFailure as exc:
              # We do not know if the commit was successfully applied on the
              # server or if it satisfied the provided write concern, set the
              # unknown commit error label.
              exc._remove_error_label("TransientTransactionError")
              _reraise_with_unknown_commit(exc)
          except WTimeoutError as exc:
              # We do not know if the commit has satisfied the provided write
              # concern, add the unknown commit error label.
              _reraise_with_unknown_commit(exc)
          except OperationFailure as exc:
              if exc.code not in _UNKNOWN_COMMIT_ERROR_CODES:
                  # The server reports errorLabels in the case.
                  raise
              # We do not know if the commit was successfully applied on the
              # server or if it satisfied the provided write concern, set the
              # unknown commit error label.
              _reraise_with_unknown_commit(exc)
          finally:
              self._transaction.state = _TxnState.COMMITTED
docs: Commit a multi-statement transaction.
  
          .. versionadded:: 3.7