@@ -14,15 +14,15 @@ internal class TdsParserSessionPool
1414 // NOTE: This is a very simplistic, lightweight pooler. It wasn't
1515 // intended to handle huge number of items, just to keep track
1616 // of the session objects to ensure that they're cleaned up in
17- // a timely manner, to avoid holding on to an unacceptible
17+ // a timely manner, to avoid holding on to an unacceptable
1818 // amount of server-side resources in the event that consumers
1919 // let their data readers be GC'd, instead of explicitly
2020 // closing or disposing of them
2121
2222 private const int MaxInactiveCount = 10 ; // pick something, preferably small...
2323
24- private static int _objectTypeCount ; // EventSource Counter
25- private readonly int _objectID = System . Threading . Interlocked . Increment ( ref _objectTypeCount ) ;
24+ private static int s_objectTypeCount ; // EventSource Counter
25+ private readonly int _objectID = System . Threading . Interlocked . Increment ( ref s_objectTypeCount ) ;
2626
2727 private readonly TdsParser _parser ; // parser that owns us
2828 private readonly List < TdsParserStateObject > _cache ; // collection of all known sessions
@@ -89,23 +89,6 @@ internal void Deactivate()
8989 }
9090 }
9191
92- // This is called from a ThreadAbort - ensure that it can be run from a CER Catch
93- internal void BestEffortCleanup ( )
94- {
95- for ( int i = 0 ; i < _cache . Count ; i ++ )
96- {
97- TdsParserStateObject session = _cache [ i ] ;
98- if ( null != session )
99- {
100- var sessionHandle = session . Handle ;
101- if ( sessionHandle != null )
102- {
103- sessionHandle . Dispose ( ) ;
104- }
105- }
106- }
107- }
108-
10992 internal void Dispose ( )
11093 {
11194 SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<sc.TdsParserSessionPool.Dispose|ADV> {0} disposing cachedCount={1}" , ObjectID , _cachedCount ) ;
@@ -140,7 +123,6 @@ internal void Dispose()
140123 }
141124 _cache . Clear ( ) ;
142125 _cachedCount = 0 ;
143-
144126 // Any active sessions will take care of themselves
145127 // (It's too dangerous to dispose them, as this can cause AVs)
146128 }
@@ -175,6 +157,7 @@ internal TdsParserStateObject GetSession(object owner)
175157
176158 session . Activate ( owner ) ;
177159 }
160+
178161 SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<sc.TdsParserSessionPool.GetSession|ADV> {0} using session {1}" , ObjectID , session . ObjectID ) ;
179162 return session ;
180163 }
@@ -190,16 +173,19 @@ internal void PutSession(TdsParserStateObject session)
190173 {
191174 if ( IsDisposed )
192175 {
193- // We're diposed - just clean out the session
176+ // We're disposed - just clean out the session
194177 Debug . Assert ( _cachedCount == 0 , "SessionPool is disposed, but there are still sessions in the cache?" ) ;
195178 session . Dispose ( ) ;
196179 }
197180 else if ( ( okToReuse ) & & ( _freeStateObjectCount < MaxInactiveCount ) )
198181 {
199182 // Session is good to re-use and our cache has space
200183 SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<sc.TdsParserSessionPool.PutSession|ADV> {0} keeping session {1} cachedCount={2}" , ObjectID , session . ObjectID , _cachedCount ) ;
184+ #if NETFRAMEWORK
201185 Debug . Assert ( ! session . _pendingData , "pending data on a pooled session?" ) ;
202-
186+ #else
187+ Debug . Assert ( ! session . HasPendingData , "pending data on a pooled session?" ) ;
188+ #endif
203189 _freeStateObjects [ _freeStateObjectCount ] = session ;
204190 _freeStateObjectCount ++ ;
205191 }
@@ -218,23 +204,37 @@ internal void PutSession(TdsParserStateObject session)
218204 }
219205 }
220206
207+
208+ internal int ActiveSessionsCount => _cachedCount - _freeStateObjectCount ;
209+
221210 internal string TraceString ( )
222211 {
223- return String . Format ( /*IFormatProvider*/ null ,
212+ return string . Format ( /*IFormatProvider*/ null ,
224213 "(ObjID={0}, free={1}, cached={2}, total={3})" ,
225214 _objectID ,
226215 null == _freeStateObjects ? "(null)" : _freeStateObjectCount . ToString ( ( IFormatProvider ) null ) ,
227216 _cachedCount ,
228217 _cache . Count ) ;
229218 }
230219
231- internal int ActiveSessionsCount
220+ #if NETFRAMEWORK
221+ // This is called from a ThreadAbort - ensure that it can be run from a CER Catch
222+ internal void BestEffortCleanup ( )
232223 {
233- get
224+ for ( int i = 0 ; i < _cache . Count ; i ++ )
234225 {
235- return _cachedCount - _freeStateObjectCount ;
226+ TdsParserStateObject session = _cache [ i ] ;
227+ if ( null != session )
228+ {
229+ SNIHandle sessionHandle = session . Handle ;
230+ if ( sessionHandle != null )
231+ {
232+ sessionHandle . Dispose ( ) ;
233+ }
234+ }
236235 }
237236 }
237+ #endif
238238 }
239239}
240240
0 commit comments