@@ -31,72 +31,111 @@ internal sealed class EndTransactionReadWriteBinding : IReadWriteBinding
3131#pragma warning restore CA2213 // Disposable fields should be disposed
3232 private bool _disposed ;
3333 private IReadWriteBindingHandle _innerBinding ;
34- private readonly ICoreSessionHandle _session ;
3534
36- public EndTransactionReadWriteBinding ( IClusterInternal cluster , ICoreSessionHandle session )
35+ public EndTransactionReadWriteBinding ( IClusterInternal cluster )
3736 {
3837 _cluster = Ensure . IsNotNull ( cluster , nameof ( cluster ) ) ;
39- _session = Ensure . IsNotNull ( session , nameof ( session ) ) ;
40- _innerBinding = ChannelPinningHelper . CreateReadWriteBinding ( _cluster , _session . Fork ( ) ) ;
4138 }
4239
4340 public ReadPreference ReadPreference => ReadPreference . Primary ;
4441
45- public ICoreSessionHandle Session => _session ;
46-
4742 // Called by EndTransactionOperation.OnRetry between attempts.
4843 public void RebuildInnerBinding ( )
4944 {
5045 ThrowIfDisposed ( ) ;
51- _innerBinding . Dispose ( ) ;
52- _innerBinding = ChannelPinningHelper . CreateReadWriteBinding ( _cluster , _session . Fork ( ) ) ;
46+ _innerBinding ? . Dispose ( ) ;
47+ _innerBinding = null ;
5348 }
5449
5550 public void Dispose ( )
5651 {
5752 if ( ! _disposed )
5853 {
59- _innerBinding . Dispose ( ) ;
60- _session . Dispose ( ) ;
54+ _innerBinding ? . Dispose ( ) ;
6155 _disposed = true ;
6256 }
6357 }
6458
6559 public IChannelSourceHandle GetReadChannelSource ( OperationContext operationContext )
66- => _innerBinding . GetReadChannelSource ( operationContext ) ;
60+ {
61+ EnsureInnerBinding ( operationContext ) ;
62+ return _innerBinding . GetReadChannelSource ( operationContext ) ;
63+ }
6764
6865 public Task < IChannelSourceHandle > GetReadChannelSourceAsync ( OperationContext operationContext )
69- => _innerBinding . GetReadChannelSourceAsync ( operationContext ) ;
66+ {
67+ EnsureInnerBinding ( operationContext ) ;
68+ return _innerBinding . GetReadChannelSourceAsync ( operationContext ) ;
69+ }
7070
7171 public IChannelSourceHandle GetReadChannelSource ( OperationContext operationContext , IReadOnlyCollection < ServerDescription > deprioritizedServers )
72- => _innerBinding . GetReadChannelSource ( operationContext , deprioritizedServers ) ;
72+ {
73+ EnsureInnerBinding ( operationContext ) ;
74+ return _innerBinding . GetReadChannelSource ( operationContext , deprioritizedServers ) ;
75+ }
7376
7477 public Task < IChannelSourceHandle > GetReadChannelSourceAsync ( OperationContext operationContext , IReadOnlyCollection < ServerDescription > deprioritizedServers )
75- => _innerBinding . GetReadChannelSourceAsync ( operationContext , deprioritizedServers ) ;
78+ {
79+ EnsureInnerBinding ( operationContext ) ;
80+ return _innerBinding . GetReadChannelSourceAsync ( operationContext , deprioritizedServers ) ;
81+ }
7682
7783 public IChannelSourceHandle GetWriteChannelSource ( OperationContext operationContext )
78- => _innerBinding . GetWriteChannelSource ( operationContext ) ;
84+ {
85+ EnsureInnerBinding ( operationContext ) ;
86+ return _innerBinding . GetWriteChannelSource ( operationContext ) ;
87+ }
7988
8089 public IChannelSourceHandle GetWriteChannelSource ( OperationContext operationContext , IReadOnlyCollection < ServerDescription > deprioritizedServers )
81- => _innerBinding . GetWriteChannelSource ( operationContext , deprioritizedServers ) ;
90+ {
91+ EnsureInnerBinding ( operationContext ) ;
92+ return _innerBinding . GetWriteChannelSource ( operationContext , deprioritizedServers ) ;
93+ }
8294
8395 public IChannelSourceHandle GetWriteChannelSource ( OperationContext operationContext , IMayUseSecondaryCriteria mayUseSecondary )
84- => _innerBinding . GetWriteChannelSource ( operationContext , mayUseSecondary ) ;
96+ {
97+ EnsureInnerBinding ( operationContext ) ;
98+ return _innerBinding . GetWriteChannelSource ( operationContext , mayUseSecondary ) ;
99+ }
85100
86101 public IChannelSourceHandle GetWriteChannelSource ( OperationContext operationContext , IReadOnlyCollection < ServerDescription > deprioritizedServers , IMayUseSecondaryCriteria mayUseSecondary )
87- => _innerBinding . GetWriteChannelSource ( operationContext , deprioritizedServers , mayUseSecondary ) ;
102+ {
103+ EnsureInnerBinding ( operationContext ) ;
104+ return _innerBinding . GetWriteChannelSource ( operationContext , deprioritizedServers , mayUseSecondary ) ;
105+ }
88106
89107 public Task < IChannelSourceHandle > GetWriteChannelSourceAsync ( OperationContext operationContext )
90- => _innerBinding . GetWriteChannelSourceAsync ( operationContext ) ;
108+ {
109+ EnsureInnerBinding ( operationContext ) ;
110+ return _innerBinding . GetWriteChannelSourceAsync ( operationContext ) ;
111+ }
91112
92113 public Task < IChannelSourceHandle > GetWriteChannelSourceAsync ( OperationContext operationContext , IReadOnlyCollection < ServerDescription > deprioritizedServers )
93- => _innerBinding . GetWriteChannelSourceAsync ( operationContext , deprioritizedServers ) ;
114+ {
115+ EnsureInnerBinding ( operationContext ) ;
116+ return _innerBinding . GetWriteChannelSourceAsync ( operationContext , deprioritizedServers ) ;
117+ }
94118
95119 public Task < IChannelSourceHandle > GetWriteChannelSourceAsync ( OperationContext operationContext , IMayUseSecondaryCriteria mayUseSecondary )
96- => _innerBinding . GetWriteChannelSourceAsync ( operationContext , mayUseSecondary ) ;
120+ {
121+ EnsureInnerBinding ( operationContext ) ;
122+ return _innerBinding . GetWriteChannelSourceAsync ( operationContext , mayUseSecondary ) ;
123+ }
97124
98125 public Task < IChannelSourceHandle > GetWriteChannelSourceAsync ( OperationContext operationContext , IReadOnlyCollection < ServerDescription > deprioritizedServers , IMayUseSecondaryCriteria mayUseSecondary )
99- => _innerBinding . GetWriteChannelSourceAsync ( operationContext , deprioritizedServers , mayUseSecondary ) ;
126+ {
127+ EnsureInnerBinding ( operationContext ) ;
128+ return _innerBinding . GetWriteChannelSourceAsync ( operationContext , deprioritizedServers , mayUseSecondary ) ;
129+ }
130+
131+ private void EnsureInnerBinding ( OperationContext operationContext )
132+ {
133+ ThrowIfDisposed ( ) ;
134+ if ( _innerBinding == null )
135+ {
136+ _innerBinding = ChannelPinningHelper . CreateReadWriteBinding ( _cluster , operationContext . Session ) ;
137+ }
138+ }
100139
101140 private void ThrowIfDisposed ( )
102141 {
0 commit comments