我有一个ASP.NET网站。如果我请求一个页面,它在大多数情况下是有效的,但有时我会得到一个HttpUnhandledException。
我已尝试记录错误,但从错误消息中我无法解决问题。
StackTrace:
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\4e215a3c\72ef69da\App_Web_ylvnbciw.6.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)数据:
System.Collections.ListDictionaryInternalBaseException:
System.InvalidOperationException: The connection was not closed. The connection's current state is open.
at DbCategory.getParentCategories()
at _Default.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)TargetSite:
Boolean HandleError(System.Exception)我知道这是关于我的会话或获取变量,但我不确定这一点。有没有人知道它会是什么?
发布于 2010-04-21 01:50:10
这样的错误不是因为错误的处理-连接没有关闭。
这样的错误可能是你在保持开放连接的静态类或方法中使用了SqlConnection,之后-在非静态中-在这一刻可能会发生异常。
更新:这个方法并不是很节省--它做的正是我所说的--他创建了连接,但从不释放它。SqlConnection是非常重的对象,您应该在获取或设置数据后立即处理它。
您应该重写您的逻辑-为每个页面或每个操作创建一个连接,并且永远不要将该连接存储在类中。
如果这对您来说是不可能的,请使用lock语句来确保单例,如下所示:
private static object singleton;
lock (singleton)
{
// Some manipulations with your server
}发布于 2010-04-21 01:33:57
一个不错的常见实践是使用Db对象(DbConnection、DbCommand、读取器、适配器等)。在使用块时。
这将防止99%的这类事情发生,通过确保对象总是在你完成使用它们时被释放。
发布于 2010-04-21 02:52:03
VMAtm:也许你得到了一些东西,因为在网站获得越来越多的用户之后,异常显示了更多的内容。我有一个静态类来保存使用单例方法的数据库连接……
public static SqlConnection getDbConnection()
{
if (sqlConn == null)
{
source = "data source=...";
sqlConn = new SqlConnection(source);
}
return sqlConn;
}这个方法是完全保存的吗?
所有数据库调用都是在使用静态方法的抽象类中调用的,这些抽象类从singleton方法获得db连接。
https://stackoverflow.com/questions/2677234
复制相似问题