Application_Start()
我從數據庫加載一些靜態數據以供進一步使用。但問題是,當服務器處於空閒狀態,例如1天,然後到DB的第一個請求失敗並返回此錯誤:ASP.NET首次在我的應用程序中訪問數據庫錯誤
The client was unable to establish a connection because of an error during connection initialization process before login.
Possible causes include the following:
the client tried to connect to an unsupported version of SQL Server;
the server was too busy to accept new connections;
or there was a resource limitation (insufficient memory or maximum allowed connections) on the server.
(provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
然後在刷新後,一切工作正常。那麼有什麼想法?爲什麼每天都要撥打Application_Start()
?我認爲只有在第一次請求應用程序時才調用它,然後才從IIS重新啓動應用程序。
編輯:
我使用SQL Server 2008R2表達
不應該有在我的代碼,我認爲問題的,但是她是:
public static SqlConnection sqlCon()
{
return new SqlConnection(WebConfigurationManager.ConnectionStrings["Test"].ConnectionString);
}
public void fillFromDB()
{
SqlConnection con = defVal.sqlCon();
SqlCommand cmd_getK = new SqlCommand("usp_getAllKomponenty", con);
cmd_getK.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
SqlDataReader reader = cmd_getK.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
this.adKomp(reader[0].ToString(), new elisKomponent(reader[0].ToString(), reader[1].ToString(), new date(Convert.ToInt32(reader[2]))));
}
reader.Close();
}
}
catch (SqlException e)
{
throw new ApplicationException("Chyba při čtení komponenty databáze"+e.Message);
}
finally
{
con.Close();
}
}
您正在使用哪個版本的sql server? –
你用來收集這些數據的代碼在哪裏?感覺就像你有一個你正在存儲的'SqlConnection'的實例**(你不應該這樣做)**,並試圖在以後再利用它。 *總是創建新的並正確處理'SqlConnection'對象。* –
查找「using」語句。你會想這樣...所以你得到IDispose.Dispose被調用。使用你的連接,然後把它們處理掉...........不要試圖去想它......讓連接池爲你處理巫術......尤其是在一個asp.net網站上。 – granadaCoder