@@ -640,158 +640,6 @@ const endTransactionAsync = promisify(
640
640
callback : ( error : Error ) => void
641
641
) => void
642
642
) ;
643
- async function endTransaction (
644
- session : ClientSession ,
645
- commandName : 'abortTransaction' | 'commitTransaction'
646
- ) : Promise < void > {
647
- // handle any initial problematic cases
648
- const txnState = session . transaction . state ;
649
-
650
- if ( txnState === TxnState . NO_TRANSACTION ) {
651
- throw new MongoTransactionError ( 'No transaction started' ) ;
652
- }
653
-
654
- if ( commandName === 'commitTransaction' ) {
655
- if (
656
- txnState === TxnState . STARTING_TRANSACTION ||
657
- txnState === TxnState . TRANSACTION_COMMITTED_EMPTY
658
- ) {
659
- // the transaction was never started, we can safely exit here
660
- session . transaction . transition ( TxnState . TRANSACTION_COMMITTED_EMPTY ) ;
661
- return ;
662
- }
663
-
664
- if ( txnState === TxnState . TRANSACTION_ABORTED ) {
665
- throw new MongoTransactionError (
666
- 'Cannot call commitTransaction after calling abortTransaction'
667
- ) ;
668
- }
669
- } else {
670
- if ( txnState === TxnState . STARTING_TRANSACTION ) {
671
- // the transaction was never started, we can safely exit here
672
- session . transaction . transition ( TxnState . TRANSACTION_ABORTED ) ;
673
- return ;
674
- }
675
-
676
- if ( txnState === TxnState . TRANSACTION_ABORTED ) {
677
- throw new MongoTransactionError ( 'Cannot call abortTransaction twice' ) ;
678
- }
679
-
680
- if (
681
- txnState === TxnState . TRANSACTION_COMMITTED ||
682
- txnState === TxnState . TRANSACTION_COMMITTED_EMPTY
683
- ) {
684
- throw new MongoTransactionError (
685
- 'Cannot call abortTransaction after calling commitTransaction'
686
- ) ;
687
- }
688
- }
689
-
690
- // construct and send the command
691
- const command : Document = { [ commandName ] : 1 } ;
692
-
693
- // apply a writeConcern if specified
694
- let writeConcern ;
695
- if ( session . transaction . options . writeConcern ) {
696
- writeConcern = Object . assign ( { } , session . transaction . options . writeConcern ) ;
697
- } else if ( session . clientOptions && session . clientOptions . writeConcern ) {
698
- writeConcern = { w : session . clientOptions . writeConcern . w } ;
699
- }
700
-
701
- if ( txnState === TxnState . TRANSACTION_COMMITTED ) {
702
- writeConcern = Object . assign ( { wtimeoutMS : 10000 } , writeConcern , { w : 'majority' } ) ;
703
- }
704
-
705
- if ( writeConcern ) {
706
- WriteConcern . apply ( command , writeConcern ) ;
707
- }
708
-
709
- if ( commandName === 'commitTransaction' && session . transaction . options . maxTimeMS ) {
710
- Object . assign ( command , { maxTimeMS : session . transaction . options . maxTimeMS } ) ;
711
- }
712
-
713
- function commandHandler ( error ?: Error ) {
714
- if ( commandName !== 'commitTransaction' ) {
715
- session . transaction . transition ( TxnState . TRANSACTION_ABORTED ) ;
716
- if ( session . loadBalanced ) {
717
- maybeClearPinnedConnection ( session , { force : false } ) ;
718
- }
719
-
720
- // The spec indicates that we should ignore all errors on `abortTransaction`
721
- return ;
722
- }
723
-
724
- session . transaction . transition ( TxnState . TRANSACTION_COMMITTED ) ;
725
- if ( error instanceof MongoError ) {
726
- if (
727
- isRetryableWriteError ( error ) ||
728
- error instanceof MongoWriteConcernError ||
729
- isMaxTimeMSExpiredError ( error )
730
- ) {
731
- if ( isUnknownTransactionCommitResult ( error ) ) {
732
- error . addErrorLabel ( MongoErrorLabel . UnknownTransactionCommitResult ) ;
733
-
734
- // per txns spec, must unpin session in this case
735
- session . unpin ( { error } ) ;
736
- }
737
- } else if ( error . hasErrorLabel ( MongoErrorLabel . TransientTransactionError ) ) {
738
- session . unpin ( { error } ) ;
739
- }
740
- }
741
-
742
- throw error ;
743
- }
744
-
745
- if ( session . transaction . recoveryToken ) {
746
- command . recoveryToken = session . transaction . recoveryToken ;
747
- }
748
-
749
- const handleFirstCommandAttempt = ( error ?: Error ) => {
750
- if ( command . abortTransaction ) {
751
- // always unpin on abort regardless of command outcome
752
- session . unpin ( ) ;
753
- }
754
-
755
- if ( error instanceof MongoError && isRetryableWriteError ( error ) ) {
756
- // SPEC-1185: apply majority write concern when retrying commitTransaction
757
- if ( command . commitTransaction ) {
758
- // per txns spec, must unpin session in this case
759
- session . unpin ( { force : true } ) ;
760
-
761
- command . writeConcern = Object . assign ( { wtimeout : 10000 } , command . writeConcern , {
762
- w : 'majority'
763
- } ) ;
764
- }
765
-
766
- executeOperation (
767
- session . client ,
768
- new RunAdminCommandOperation ( command , {
769
- session,
770
- readPreference : ReadPreference . primary ,
771
- bypassPinningCheck : true
772
- } )
773
- ) . then ( ( ) => commandHandler ( ) , commandHandler ) ;
774
- return ;
775
- }
776
-
777
- commandHandler ( error ) ;
778
- } ;
779
-
780
- // send the command
781
- try {
782
- await executeOperation (
783
- session . client ,
784
- new RunAdminCommandOperation ( command , {
785
- session,
786
- readPreference : ReadPreference . primary ,
787
- bypassPinningCheck : true
788
- } )
789
- ) ;
790
- handleFirstCommandAttempt ( ) ;
791
- } catch ( e ) {
792
- handleFirstCommandAttempt ( e ) ;
793
- }
794
- }
795
643
796
644
function endTransaction (
797
645
session : ClientSession ,
0 commit comments