@@ -199,7 +199,7 @@ public override void UpdateParameters(in global::Dapper.UnifiedCommand cmd, obje
199199 public override global ::System . Data . Common . DbCommand GetCommand ( global ::System . Data . Common . DbConnection connection ,
200200 string sql , global ::System . Data . CommandType commandType , object ? args )
201201 {
202- var cmd = TryReuse ( ref Storage , sql , commandType , args ) ;
202+ var cmd = TryReuseThreadStatic ( ref Storage , sql , commandType , args , _cmdPool ) ;
203203 if ( cmd is null )
204204 {
205205 cmd = base . GetCommand ( connection , sql , commandType , args ) ;
@@ -212,7 +212,9 @@ public override void UpdateParameters(in global::Dapper.UnifiedCommand cmd, obje
212212 return cmd ;
213213 }
214214
215- public override bool TryRecycle ( global ::System . Data . Common . DbCommand command ) => TryRecycle ( ref Storage , command ) ;
215+ public override bool TryRecycle ( global ::System . Data . Common . DbCommand command ) => TryRecycleThreadStatic ( ref Storage , command , _cmdPool ) ;
216+ private readonly DbCommandCache _cmdPool = new ( ) ;
217+ [ global ::System . ThreadStatic ] // note this works correctly with ref
216218 private static global ::System . Data . Common . DbCommand ? Storage ;
217219
218220 }
@@ -256,7 +258,7 @@ public override void UpdateParameters(in global::Dapper.UnifiedCommand cmd, obje
256258 public override global ::System . Data . Common . DbCommand GetCommand ( global ::System . Data . Common . DbConnection connection ,
257259 string sql , global ::System . Data . CommandType commandType , object ? args )
258260 {
259- var cmd = TryReuse ( ref Storage , sql , commandType , args ) ;
261+ var cmd = TryReuseThreadStatic ( ref Storage , sql , commandType , args , _cmdPool ) ;
260262 if ( cmd is null )
261263 {
262264 cmd = base . GetCommand ( connection , sql , commandType , args ) ;
@@ -269,18 +271,21 @@ public override void UpdateParameters(in global::Dapper.UnifiedCommand cmd, obje
269271 return cmd ;
270272 }
271273
272- public override bool TryRecycle ( global ::System . Data . Common . DbCommand command ) => TryRecycle ( ref Storage , command ) ;
274+ public override bool TryRecycle ( global ::System . Data . Common . DbCommand command ) => TryRecycleThreadStatic ( ref Storage , command , _cmdPool ) ;
275+ private readonly DbCommandCache _cmdPool = new ( ) ;
273276 protected abstract ref global ::System . Data . Common . DbCommand ? Storage { get ; }
274277
275278 internal sealed class Cached0 : CommandFactory1
276279 {
277280 protected override ref global ::System . Data . Common . DbCommand ? Storage => ref s_Storage ;
281+ [ global ::System . ThreadStatic ] // note this works correctly with ref-return
278282 private static global ::System . Data . Common . DbCommand ? s_Storage ;
279283
280284 }
281285 internal sealed class Cached1 : CommandFactory1
282286 {
283287 protected override ref global ::System . Data . Common . DbCommand ? Storage => ref s_Storage ;
288+ [ global ::System . ThreadStatic ] // note this works correctly with ref-return
284289 private static global ::System . Data . Common . DbCommand ? s_Storage ;
285290
286291 }
0 commit comments