Skip to content

Commit c68dd0e

Browse files
authored
Merge to shared - TdsParserSessionPool (#1595)
1 parent 1cda345 commit c68dd0e

File tree

4 files changed

+33
-254
lines changed

4 files changed

+33
-254
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@
475475
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs">
476476
<Link>Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs</Link>
477477
</Compile>
478+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSessionPool.cs">
479+
<Link>Microsoft\Data\SqlClient\TdsParserSessionPool.cs</Link>
480+
</Compile>
478481
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsValueSetter.cs">
479482
<Link>Microsoft\Data\SqlClient\TdsValueSetter.cs</Link>
480483
</Compile>
@@ -643,7 +646,6 @@
643646
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
644647
<Compile Include="Microsoft\Data\SqlClient\TdsParser.RegisterEncoding.cs" />
645648
<Compile Include="Microsoft\Data\SqlClient\TdsParserHelperClasses.cs" />
646-
<Compile Include="Microsoft\Data\SqlClient\TdsParserSessionPool.cs" />
647649
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
648650
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectManaged.cs" />
649651
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netcore.cs" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSessionPool.cs

-225
This file was deleted.

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@
566566
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs">
567567
<Link>Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs</Link>
568568
</Compile>
569+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSessionPool.cs">
570+
<Link>Microsoft\Data\SqlClient\TdsParserSessionPool.cs</Link>
571+
</Compile>
569572
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsValueSetter.cs">
570573
<Link>Microsoft\Data\SqlClient\TdsValueSetter.cs</Link>
571574
</Compile>
@@ -653,7 +656,6 @@
653656
<Compile Include="Microsoft\Data\SqlClient\SqlUtil.cs" />
654657
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
655658
<Compile Include="Microsoft\Data\SqlClient\TdsParserHelperClasses.cs" />
656-
<Compile Include="Microsoft\Data\SqlClient\TdsParserSessionPool.cs" />
657659
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
658660
<Compile Include="Microsoft\Data\SqlTypes\SqlFileStream.cs" />
659661
<Compile Include="Microsoft\Data\SqlTypes\SqlStreamChars.cs" />

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserSessionPool.cs src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSessionPool.cs

+27-27
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)