fix: 收敛旧模块资源释放与诊断
This commit is contained in:
@@ -2112,6 +2112,7 @@ namespace Lskj.Control
|
||||
public override int SetGridViewDataSource(DbDataAdapter adapter, bool selectRowHandler = true)
|
||||
{
|
||||
if (adapter == null) return 0;
|
||||
DbDataAdapter previousAdapter = this.AdapterObj;
|
||||
if (this.ColumnList == null || this.ColumnList.Count == 0)
|
||||
{
|
||||
this.gcMain.Bands.Clear();
|
||||
@@ -2122,53 +2123,74 @@ namespace Lskj.Control
|
||||
lastNodeIndex = 0;
|
||||
if (selectRowHandler) lastNodeIndex = this.TreeListObj.GetVisibleIndexByNode(this.TreeListObj.FocusedNode);
|
||||
|
||||
this.AdapterObj = adapter;
|
||||
DataSet dataSet = new DataSet();
|
||||
//adapter.Fill(dataSet, "SetGridViewDataSource");
|
||||
// 重连配置
|
||||
int maxRetryCount = 2; // 次数
|
||||
int retryDelay = 1000; // 等待时间
|
||||
for (int i = 0; i < maxRetryCount; i++)
|
||||
try
|
||||
{
|
||||
try
|
||||
for (int i = 0; i < maxRetryCount; i++)
|
||||
{
|
||||
adapter.Fill(dataSet, "SetGridViewDataSource");
|
||||
break;//连接成功
|
||||
}
|
||||
catch (SqlException ex)
|
||||
{
|
||||
// 判断是否是连接相关错误
|
||||
if (IsConnectionError(ex.Number) ||
|
||||
ex.Message.Contains("物理连接不可用") ||
|
||||
ex.Message.Contains("传输级错误"))
|
||||
try
|
||||
{
|
||||
if (i < maxRetryCount - 1)
|
||||
adapter.Fill(dataSet, "SetGridViewDataSource");
|
||||
break;//连接成功
|
||||
}
|
||||
catch (SqlException ex)
|
||||
{
|
||||
// 判断是否是连接相关错误
|
||||
if (IsConnectionError(ex.Number) ||
|
||||
ex.Message.Contains("物理连接不可用") ||
|
||||
ex.Message.Contains("传输级错误"))
|
||||
{
|
||||
SqlConnection.ClearAllPools();
|
||||
System.Threading.Thread.Sleep(retryDelay);
|
||||
continue; // 重试
|
||||
if (i < maxRetryCount - 1)
|
||||
{
|
||||
SqlConnection.ClearAllPools();
|
||||
System.Threading.Thread.Sleep(retryDelay);
|
||||
continue; // 重试
|
||||
}
|
||||
else
|
||||
{
|
||||
// 最后一次重试失败,抛出错误
|
||||
throw new Exception($"经过 {maxRetryCount} 次重试后仍无法连接到数据库: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 最后一次重试失败,抛出错误
|
||||
throw new Exception($"经过 {maxRetryCount} 次重试后仍无法连接到数据库: {ex.Message}", ex);
|
||||
// 其他SQL错误直接抛出
|
||||
throw;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他SQL错误直接抛出
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
dataSet.Dispose();
|
||||
DisposeDataAdapter(adapter);
|
||||
throw;
|
||||
}
|
||||
|
||||
DataTable resultTable = dataSet.Tables.Count > 0 ? dataSet.Tables[0] : null;
|
||||
int rowCount = resultTable == null ? 0 : resultTable.Rows.Count;
|
||||
if (resultTable != null)
|
||||
{
|
||||
dataSet.Tables.Remove(resultTable);
|
||||
}
|
||||
dataSet.Dispose();
|
||||
|
||||
DbCommandBuilder cb = SqlHelper.dbFactory.CreateCommandBuilder();
|
||||
cb.DataAdapter = this.AdapterObj;
|
||||
DisposeAdapterCommandBuilder();
|
||||
if (previousAdapter != null && !ReferenceEquals(previousAdapter, adapter))
|
||||
{
|
||||
DisposeDataAdapter(previousAdapter);
|
||||
}
|
||||
this.AdapterObj = adapter;
|
||||
|
||||
SetAdapterCommandBuilder(this.AdapterObj);
|
||||
//SqlCommandBuilder cb = new SqlCommandBuilder(this.AdapterObj);
|
||||
|
||||
GetTreeExpanded();
|
||||
this.gcMain.DataSource = dataSet.Tables[0];
|
||||
this.gcMain.DataSource = resultTable;
|
||||
this.SetAutoColumns();
|
||||
//根据记录展开对应节点
|
||||
foreach (TreeListNode treeListNode in treeListNodes)
|
||||
@@ -2193,7 +2215,7 @@ namespace Lskj.Control
|
||||
{
|
||||
this.TreeListObj.FocusedNode = lastNode;
|
||||
}
|
||||
return dataSet.Tables[0] != null ? dataSet.Tables[0].Rows.Count : 0;
|
||||
return rowCount;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user