22,206
社区成员
发帖
与我相关
我的任务
分享
override public void Close() {
IntPtr hscp;
Bid.ScopeEnter(out hscp, "<sc.SqlConnection.Close|API> %d#" , ObjectID);
Bid.CorrelationTrace("<sc.SqlConnection.Close|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
try {
SqlStatistics statistics = null;
TdsParser bestEffortCleanupTarget = null;
RuntimeHelpers.PrepareConstrainedRegions();
try {
#if DEBUG
TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
RuntimeHelpers.PrepareConstrainedRegions();
try {
tdsReliabilitySection.Start();
#else
{
#endif //DEBUG
bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(this);
statistics = SqlStatistics.StartTimer(Statistics);
Task reconnectTask = _currentReconnectionTask;
if (reconnectTask != null && !reconnectTask.IsCompleted) {
CancellationTokenSource cts = _reconnectionCancellationSource;
if (cts != null) {
cts.Cancel();
}
AsyncHelper.WaitForCompletion(reconnectTask, 0, null, rethrowExceptions: false); // we do not need to deal with possible exceptions in reconnection
if (State != ConnectionState.Open) {// if we cancelled before the connection was opened
OnStateChange(DbConnectionInternal.StateChangeClosed);
}
}
CancelOpenAndWait();
CloseInnerConnection();
GC.SuppressFinalize(this);
if (null != Statistics) {
ADP.TimerCurrent(out _statistics._closeTimestamp);
}
}
#if DEBUG
finally {
tdsReliabilitySection.Stop();
}
#endif //DEBUG
}
catch (System.OutOfMemoryException e) {
Abort(e);
throw;
}
catch (System.StackOverflowException e) {
Abort(e);
throw;
}
catch (System.Threading.ThreadAbortException e) {
Abort(e);
SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
throw;
}
finally {
SqlStatistics.StopTimer(statistics);
}
}
finally {
SqlDebugContext sdc = _sdc;
_sdc = null;
Bid.ScopeLeave(ref hscp);
if (sdc != null) {
sdc.Dispose();
}
}
}
public override void Close()
{
SqlStatistics statistics = null;
IntPtr ptr;
Bid.ScopeEnter(out ptr, "<sc.SqlDataReader.Close|API> %d#", this.ObjectID);
try
{
statistics = SqlStatistics.StartTimer(this.Statistics);
if (!this.IsClosed)
{
this.SetTimeout();
this.CloseInternal(true); // 调用内部方法 //
}
}
finally
{
SqlStatistics.StopTimer(statistics);
Bid.ScopeLeave(ref ptr);
}
}
private void CloseInternal(bool closeReader)
{
TdsParser parser = this._parser;
TdsParserStateObject stateObj = this._stateObj;
bool flag2 = this.IsCommandBehavior(CommandBehavior.CloseConnection); // 标志 //
this._parser = null;
bool flag = false;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
if (((parser != null) && (stateObj != null)) && (stateObj._pendingData && (parser.State == TdsParserState.OpenLoggedIn)))
{
if (this._altRowStatus == ALTROWSTATUS.AltRow)
{
this._dataReady = true;
}
if (this._dataReady)
{
this.CleanPartialRead();
}
parser.Run(RunBehavior.Clean, this._command, this, null, stateObj);
}
this.RestoreServerSettings(parser, stateObj);
}
catch (OutOfMemoryException exception6)
{
this._isClosed = true;
flag = true;
if (this._connection != null)
{
this._connection.Abort(exception6);
}
throw;
}
catch (StackOverflowException exception5)
{
this._isClosed = true;
flag = true;
if (this._connection != null)
{
this._connection.Abort(exception5);
}
throw;
}
catch (ThreadAbortException exception4)
{
this._isClosed = true;
flag = true;
if (this._connection != null)
{
this._connection.Abort(exception4);
}
throw;
}
finally
{
if (flag)
{
this._isClosed = true;
this._command = null;
this._connection = null;
this._statistics = null;
}
else if (closeReader)
{
this._stateObj = null;
this._data = null;
if (this.Connection != null)
{
this.Connection.RemoveWeakReference(this);
}
RuntimeHelpers.PrepareConstrainedRegions();
try
{
if ((this._command != null) && (stateObj != null))
{
stateObj.CloseSession();
}
}
catch (OutOfMemoryException exception3)
{
this._isClosed = true;
flag = true;
if (this._connection != null)
{
this._connection.Abort(exception3);
}
throw;
}
catch (StackOverflowException exception2)
{
this._isClosed = true;
flag = true;
if (this._connection != null)
{
this._connection.Abort(exception2);
}
throw;
}
catch (ThreadAbortException exception)
{
this._isClosed = true;
flag = true;
if (this._connection != null)
{
this._connection.Abort(exception);
}
throw;
}
this.SetMetaData(null, false);
this._dataReady = false;
this._isClosed = true;
this._fieldNameLookup = null;
if (flag2 && (this.Connection != null))
{
this.Connection.Close(); // 自动关闭连接 //
}
if (this._command != null)
{
this._recordsAffected = this._command.InternalRecordsAffected;
}
this._command = null;
this._connection = null;
this._statistics = null;
}
}
}