GridView에 레코드 목록을 표시하고 여러 체크 박스를 선택하여 레코드를 대량 삭제할 수있는 웹 양식 앱이 있습니다. 코드 자체는 단순하다풀에서 연결이 끊어졌습니다.
프로필 생성자protected void btnDelete_Click(object sender, EventArgs e)
{
int i = 0;
try
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("ID");
if (cb != null && cb.Checked)
{
int profileID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
Profile profile = new Profile(profileID); //instantiate profile
profile.Delete(); //call delete method
i++;
}
}
if (i > 0)
{
//report success to UI
}
}
catch (Exception ex)
{
//report error to UI
}
}
, 그것은, 연결을 열기를 DataReader를 열고 그 오브젝트의 속성을 설정하여 객체를 촉촉. 난 내 코드에서 using()
블록에 대한 세심한 그래서 모든 DB 연결이 같은 대한 보인다 이니
using (SQLHelper db = new SQLHelper())
{
db.AddParameterToSQLCommand("@ProfileID", SqlDbType.Int);
db.SetSQLCommandParameterValue("@ProfileID", id);
using (SqlDataReader dr = db.GetReaderByCmd("up_GetProfile"))
{
if (dr.Read())
{
_profileID = id;
if (!dr.IsDBNull(0))
ProfileName = dr.GetString(0);
//... and so on
return true;
}
else
{
return false;
}
}
}
내 헬퍼 클래스처럼 DataReader를이 iDisposible을 구현하고, 소멸자는 다음과 같습니다
public void Dispose()
{
try
{
//Clean Up Connection Object
if (mobj_SqlConnection != null)
{
if (mobj_SqlConnection.State != ConnectionState.Closed)
{
mobj_SqlConnection.Close();
}
mobj_SqlConnection.Dispose();
}
//Clean Up Command Object
if (mobj_SqlCommand != null)
{
mobj_SqlCommand.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("Error disposing data class." + Environment.NewLine + ex.Message);
}
}
내 코드를 단계별로 실행하면 연결이 항상 열리고 올바르게 닫힌다는 것을 알 수 있습니다. 내 스택은 5,6 콜을 넘지 않습니다. (모든 재귀 문제가 발생하지 않습니다.) 모든 데이터 액세스 코드가 올바르게 블록으로 묶여 있지만 연결은 풀로 다시 릴리스되지 않습니다. 대신이 오류를 얻을 : 이것은이 10 명 + 프로필을 삭제 초래 한 사용자와 전용 응용 프로그램 풀에서 발생
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.
합니다. 은 내가 올바르게 모든 것을하고있는 것처럼 인 것 같습니다. 그러나 연결이 풀에 다시 릴리스되지 않는 이유는 무엇입니까? 에서 대부분은 실행 스레드에 의해 열린 두 개의 연결이어야합니다. 둘 다 범위를 벗어나면 처분해야합니다.
나는 분명히 잘못된 일을하고 있지만 내 삶을 위해 할 수없는 것은 무엇인지 알아 낸다.
이 있어야 할 곳에 무언가가 종료되지 않습니다. Connection에서 코드를 정리하기 전에 Command 객체에서 정리 코드를 옮겨보십시오. 또한 사용하는 경우 DataAdapters. – LarsTech
도움이 된 것으로 보이는 @LarsTech, 나는 그 변화를 만들고 재배포 한 후에 더 이상 오류를 재현 할 수 없다. –