33using System . Diagnostics . CodeAnalysis ;
44using System . Text ;
55using Ydb . Sdk . Ado . Internal ;
6- using Ydb . Sdk . Ado . Session ;
76
87namespace Ydb . Sdk . Ado ;
98
@@ -166,7 +165,8 @@ protected override DbTransaction? DbTransaction
166165 protected override YdbDataReader ExecuteDbDataReader ( CommandBehavior behavior ) =>
167166 ExecuteReaderAsync ( behavior ) . GetAwaiter ( ) . GetResult ( ) ;
168167
169- protected override async Task < DbDataReader > ExecuteDbDataReaderAsync ( CommandBehavior behavior ,
168+ protected override async Task < DbDataReader > ExecuteDbDataReaderAsync (
169+ CommandBehavior behavior ,
170170 CancellationToken cancellationToken )
171171 {
172172 cancellationToken . ThrowIfCancellationRequested ( ) ;
@@ -210,47 +210,28 @@ protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBeha
210210 ? new GrpcRequestSettings { TransportTimeout = TimeSpan . FromSeconds ( CommandTimeout ) }
211211 : new GrpcRequestSettings ( ) ;
212212
213- var connectionTx = YdbConnection . CurrentTransaction ;
214- var effectiveTx = Transaction ?? connectionTx ;
213+ var transaction = YdbConnection . CurrentTransaction ;
215214
216- if ( Transaction != null && Transaction != connectionTx )
215+ if ( Transaction != null && Transaction != transaction ) // assert on legacy DbTransaction property
217216 {
218217 throw new InvalidOperationException ( "Transaction mismatched! (Maybe using another connection)" ) ;
219218 }
220219
221- var useImplicit = YdbConnection . EnableImplicitSession && effectiveTx is null ;
222- var session = useImplicit
223- ? new ImplicitSession ( YdbConnection . Session . Driver , new ImplicitSessionSource ( YdbConnection . Session . Driver ) )
224- : YdbConnection . Session ;
220+ var useImplicit = transaction is null && YdbConnection . EnableImplicitSession ;
221+ var execSession = YdbConnection . GetExecutionSession ( useImplicit ) ;
225222
226- YdbDataReader ydbDataReader ;
227- try
228- {
229- if ( effectiveTx is not null && session is ImplicitSession )
230- {
231- throw new InvalidOperationException (
232- "Invariant violated: pooled session expected inside a transaction, but ImplicitSession was selected." ) ;
233- }
234-
235- var execResult = await session . ExecuteQuery (
223+ var ydbDataReader = await YdbDataReader . CreateYdbDataReader (
224+ await execSession . ExecuteQuery (
236225 preparedSql . ToString ( ) ,
237226 ydbParameters ,
238227 execSettings ,
239- useImplicit ? null : effectiveTx ? . TransactionControl
240- ) ;
241-
242- ydbDataReader = await YdbDataReader . CreateYdbDataReader (
243- execResult ,
244- YdbConnection . OnNotSuccessStatusCode ,
245- effectiveTx ,
246- cancellationToken
247- ) ;
248- }
249- finally
250- {
251- if ( useImplicit )
252- session . Dispose ( ) ;
253- }
228+ transaction ? . TransactionControl
229+ ) ,
230+ YdbConnection . OnNotSuccessStatusCode ,
231+ transaction ,
232+ cancellationToken ) ;
233+
234+ YdbConnection . AdoptSession ( execSession ) ;
254235
255236 YdbConnection . LastReader = ydbDataReader ;
256237 YdbConnection . LastCommand = CommandText ;
@@ -269,7 +250,8 @@ protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBeha
269250 ExecuteReaderAsync ( behavior , CancellationToken . None ) ;
270251
271252 // ReSharper disable once MemberCanBePrivate.Global
272- public new async Task < YdbDataReader > ExecuteReaderAsync ( CommandBehavior behavior ,
253+ public new async Task < YdbDataReader > ExecuteReaderAsync (
254+ CommandBehavior behavior ,
273255 CancellationToken cancellationToken ) =>
274256 ( YdbDataReader ) await ExecuteDbDataReaderAsync ( behavior , cancellationToken ) ;
275257}
0 commit comments