1616using System . Threading ;
1717using MongoDB . Driver . Core . Bindings ;
1818using MongoDB . Driver . Core . Clusters ;
19+ using MongoDB . Driver . Core . ConnectionPools ;
1920using MongoDB . Driver . Core . Connections ;
2021using MongoDB . Driver . Core . Servers ;
2122
@@ -32,9 +33,8 @@ public static class ChannelPinningHelper
3233 /// <param name="cluster">The cluster,</param>
3334 /// <param name="session">The session.</param>
3435 /// <param name="readPreference">The read preference.</param>
35- /// <param name="doesInitiateCursor">The flag whether operation initiates cursor.</param>
3636 /// <returns>An effective read binging.</returns>
37- public static IReadBindingHandle CreateReadBinding ( ICluster cluster , ICoreSessionHandle session , ReadPreference readPreference , bool doesInitiateCursor )
37+ public static IReadBindingHandle CreateReadBinding ( ICluster cluster , ICoreSessionHandle session , ReadPreference readPreference )
3838 {
3939 IReadBinding readBinding ;
4040 if ( session . IsInTransaction &&
@@ -48,21 +48,12 @@ public static IReadBindingHandle CreateReadBinding(ICluster cluster, ICoreSessio
4848 }
4949 else
5050 {
51- TrackedOperationRunContext trackedOperationRunContext ;
52- if ( IsInLoadBalancedMode ( cluster . Description ) )
51+ if ( IsInLoadBalancedMode ( cluster . Description ) && IsChannelPinned ( session . CurrentTransaction ) )
5352 {
54- if ( IsChannelPinned ( session . CurrentTransaction ) )
55- {
56- // unpin if the next operation is not under transaction
57- session . CurrentTransaction . UnpinAll ( ) ;
58- }
59- trackedOperationRunContext = new TrackedOperationRunContext ( session . IsInTransaction , doesInitiateCursor ) ;
60- }
61- else
62- {
63- trackedOperationRunContext = TrackedOperationRunContext . CreateEmpty ( ) ;
53+ // unpin if the next operation is not under transaction
54+ session . CurrentTransaction . UnpinAll ( ) ;
6455 }
65- readBinding = new ReadPreferenceBinding ( cluster , readPreference , session , trackedOperationRunContext ) ;
56+ readBinding = new ReadPreferenceBinding ( cluster , readPreference , session ) ;
6657 }
6758
6859 return new ReadBindingHandle ( readBinding ) ;
@@ -88,23 +79,12 @@ public static IReadWriteBindingHandle CreateReadWriteBinding(ICluster cluster, I
8879 }
8980 else
9081 {
91- TrackedOperationRunContext trackedOperationRunContext ;
92- if ( IsInLoadBalancedMode ( cluster . Description ) )
93- {
94- if ( IsChannelPinned ( session . CurrentTransaction ) )
95- {
96- // unpin if the next operation is not under transaction
97- session . CurrentTransaction . UnpinAll ( ) ;
98- }
99-
100- trackedOperationRunContext = new TrackedOperationRunContext ( session . IsInTransaction , withCursorResult : false ) ;
101- }
102- else
82+ if ( IsInLoadBalancedMode ( cluster . Description ) && IsChannelPinned ( session . CurrentTransaction ) )
10383 {
104- trackedOperationRunContext = TrackedOperationRunContext . CreateEmpty ( ) ;
84+ // unpin if the next operation is not under transaction
85+ session . CurrentTransaction . UnpinAll ( ) ;
10586 }
106-
107- readWriteBinding = new WritableServerBinding ( cluster , session , trackedOperationRunContext ) ;
87+ readWriteBinding = new WritableServerBinding ( cluster , session ) ;
10888 }
10989
11090 return new ReadWriteBindingHandle ( readWriteBinding ) ;
@@ -117,6 +97,10 @@ internal static IChannelSourceHandle CreateGetMoreChannelSource(IChannelSourceHa
11797 {
11898 var getMoreChannel = channelSource . GetChannel ( CancellationToken . None ) ; // no need for cancellation token since we already have channel in the source
11999 var getMoreSession = channelSource . Session . Fork ( ) ;
100+ if ( getMoreChannel . Connection is ITrackedPinningReason trackedConnection )
101+ {
102+ trackedConnection . SetPinningCheckoutReasonIfNotAlreadySet ( CheckedOutReason . Cursor ) ;
103+ }
120104
121105 effectiveChannelSource = new ChannelChannelSource (
122106 channelSource . Server ,
@@ -150,6 +134,10 @@ internal static bool PinChannelSourceAndChannelIfRequired(
150134
151135 if ( session . IsInTransaction && ! IsChannelPinned ( session . CurrentTransaction ) )
152136 {
137+ if ( channel . Connection is ITrackedPinningReason trackedConnection )
138+ {
139+ trackedConnection . SetPinningCheckoutReasonIfNotAlreadySet ( CheckedOutReason . Transaction ) ;
140+ }
153141 session . CurrentTransaction . PinChannel ( channel . Fork ( ) ) ;
154142 session . CurrentTransaction . PinnedServer = server ;
155143 }
0 commit comments