반응형
C# 평행.각 풀에서 너무 많은 연결을 여는 경우
수신되는 전체 오류는 다음과 같습니다.
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
다음과 같은 유사점이 있습니다.
Parallel.ForEach(fields, item =>
{
item.SysInvoiceID = _destinationRepo.GetMySQLSystemID(item);
});
이는 다음과 같은 방법을 호출합니다.
public int GetMySQLSystemID(CustomFieldBase GenericCustomField)
{
CustomField customField = GenericCustomField as CustomField;
int sys_InvoiceID = 0;
using (MySqlConnection mySQLConnection = new MySqlConnection("Server=web01;Database=wmp;User Name=root;Password=TotallyMyPWord"))
{
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.CommandText = $@"SELECT Sys_InvoiceID FROM tblsys_naturalkey_lu WHERE CompanyCode = '{customField.CompanyCode}' AND InvoiceNo = '{customField.InvoiceNo}'
AND LineItemNo = '{customField.LineItemNo}' AND FiscalYear = '{customField.FiscalYear}'";
cmd.CommandType = CommandType.Text;
cmd.Connection = mySQLConnection;
mySQLConnection.Open();
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
sys_InvoiceID = (int)reader["Sys_InvoiceID"];
}
}
mySQLConnection.Close();
}
}
return sys_InvoiceID;
}
MySQL Session Manager를 보면 다음과 같은 것을 알 수 있습니다.Parallel.ForEach한계가 있을 때까지 계속 연결을 추가합니다.
MySQL Connection을 닫으려고 하는데, 최대 한도까지 연결을 계속 만드는 이유는 무엇입니까?
생성된 병렬 작업 수에 제한을 두지 않았습니다.Parallel.ForEach()따라서 무한한 수의 SQL 연결을 생성할 수 있습니다.
최대 병렬화 정도를 지정하여 이 문제를 해결할 수 있습니다.
Parallel.ForEach(fields, new ParallelOptions { MaxDegreeOfParallelism = 8 }, item =>
{
item.SysInvoiceID = _destinationRepo.GetMySQLSystemID(item);
});
하지만 (다른 사람들이 지적했듯이) 사용하면 안 됩니다.Parallel.ForEach()어떤 경우에도 IO 제한 작업에 사용할 수 있습니다.
SqlConnection을 사용할 수 있습니다.ClearPool 방법이지만 병렬 루프를 통해 연결해야 하는 경우에는 다시 사용할 것을 제안합니다.다른 사람들이 언급했듯이 당신은 이것을 해서는 안 됩니다.
예를 들어 병렬 루프를 시작하기 전에 연결을 엽니다.그런 다음 모든 작업이 완료된 후 폐기합니다.
언급URL : https://stackoverflow.com/questions/41062601/c-sharp-parallel-foreach-opening-up-too-many-connections-from-the-pool
반응형
'it-source' 카테고리의 다른 글
| Chrome에게 ts 대신 js를 디버그하도록 지시 (0) | 2023.06.20 |
|---|---|
| pyodbc를 사용하여 SQL에서 데이터 검색 (0) | 2023.06.20 |
| 실패: 'DynamicTestModule' 모듈에서 예기치 않은 지시문 'ContactDetailsComponent'를 가져왔습니다.@NgModule 주석을 추가하십시오. (0) | 2023.06.15 |
| Apache POI를 사용하여 Excel 파일에서 열을 가져오는 방법은 무엇입니까? (0) | 2023.06.15 |
| 잘못된 http_host 헤더 (0) | 2023.06.15 |