@@ -14,15 +14,15 @@ internal class TdsParserSessionPool
14
14
// NOTE: This is a very simplistic, lightweight pooler. It wasn't
15
15
// intended to handle huge number of items, just to keep track
16
16
// 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
18
18
// amount of server-side resources in the event that consumers
19
19
// let their data readers be GC'd, instead of explicitly
20
20
// closing or disposing of them
21
21
22
22
private const int MaxInactiveCount = 10 ; // pick something, preferably small...
23
23
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 ) ;
26
26
27
27
private readonly TdsParser _parser ; // parser that owns us
28
28
private readonly List < TdsParserStateObject > _cache ; // collection of all known sessions
@@ -89,23 +89,6 @@ internal void Deactivate()
89
89
}
90
90
}
91
91
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
-
109
92
internal void Dispose ( )
110
93
{
111
94
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<sc.TdsParserSessionPool.Dispose|ADV> {0} disposing cachedCount={1}" , ObjectID , _cachedCount ) ;
@@ -140,7 +123,6 @@ internal void Dispose()
140
123
}
141
124
_cache . Clear ( ) ;
142
125
_cachedCount = 0 ;
143
-
144
126
// Any active sessions will take care of themselves
145
127
// (It's too dangerous to dispose them, as this can cause AVs)
146
128
}
@@ -175,6 +157,7 @@ internal TdsParserStateObject GetSession(object owner)
175
157
176
158
session . Activate ( owner ) ;
177
159
}
160
+
178
161
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<sc.TdsParserSessionPool.GetSession|ADV> {0} using session {1}" , ObjectID , session . ObjectID ) ;
179
162
return session ;
180
163
}
@@ -190,16 +173,19 @@ internal void PutSession(TdsParserStateObject session)
190
173
{
191
174
if ( IsDisposed )
192
175
{
193
- // We're diposed - just clean out the session
176
+ // We're disposed - just clean out the session
194
177
Debug . Assert ( _cachedCount == 0 , "SessionPool is disposed, but there are still sessions in the cache?" ) ;
195
178
session . Dispose ( ) ;
196
179
}
197
180
else if ( ( okToReuse ) & & ( _freeStateObjectCount < MaxInactiveCount ) )
198
181
{
199
182
// Session is good to re-use and our cache has space
200
183
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<sc.TdsParserSessionPool.PutSession|ADV> {0} keeping session {1} cachedCount={2}" , ObjectID , session . ObjectID , _cachedCount ) ;
184
+ #if NETFRAMEWORK
201
185
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
203
189
_freeStateObjects [ _freeStateObjectCount ] = session ;
204
190
_freeStateObjectCount ++ ;
205
191
}
@@ -218,23 +204,37 @@ internal void PutSession(TdsParserStateObject session)
218
204
}
219
205
}
220
206
207
+
208
+ internal int ActiveSessionsCount => _cachedCount - _freeStateObjectCount ;
209
+
221
210
internal string TraceString ( )
222
211
{
223
- return String . Format ( /*IFormatProvider*/ null ,
212
+ return string . Format ( /*IFormatProvider*/ null ,
224
213
"(ObjID={0}, free={1}, cached={2}, total={3})" ,
225
214
_objectID ,
226
215
null == _freeStateObjects ? "(null)" : _freeStateObjectCount . ToString ( ( IFormatProvider ) null ) ,
227
216
_cachedCount ,
228
217
_cache . Count ) ;
229
218
}
230
219
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 ( )
232
223
{
233
- get
224
+ for ( int i = 0 ; i < _cache . Count ; i ++ )
234
225
{
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
+ }
236
235
}
237
236
}
237
+ #endif
238
238
}
239
239
}
240
240
0 commit comments