由于弃用的互斥锁,等待完成

时间:2012-07-16 09:16:04

标签: c# asp.net performance mutex request-timed-out

我从未在我的应用程序中使用过mutex而我现在不知道它做了什么,但我的Web应用程序抛出了以下异常,我不知道如何处理它:

   IP: System.Web.HttpApplicationState
   07/16/2012 10:06:01
   Error in: https://-------/Loginpage.aspx
   Error Message:Error in:https://--------/Loginpage.aspx   Error Message:The wait completed due to an abandoned mutex.  Stack Trace :    at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext)
   at System.Threading.WaitHandle.WaitOne(Int32 millisecondsTimeout, Boolean exitContext)
   at System.Threading.WaitHandle.WaitOne()
   at IBM.Data.Informix.IfxConnection.GetLatch(String callerMsg)
   at IBM.Data.Informix.IfxCommand.ExecuteReaderObject(CommandBehavior behavior, String method)
   at IBM.Data.Informix.IfxCommand.ExecuteReader(CommandBehavior behavior)
   at IBM.Data.Informix.IfxCommand.ExecuteReader()
   at DB_Connection_s.DB_Connection.IsValidPortalUser(String p_u, String p_p)
   at LoginSystem.LoginPage.ValidateUser(String UserName, String Password) in H:\LoginSystem\LoginSystem\LoginPage.aspx.cs:line 21
   at LoginSystem.LoginPage.ibtn_login_Click(Object sender, ImageClickEventArgs e) in H:\LoginSystem\LoginSystem\LoginPage.aspx.cs:line 34
   at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
   at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

public static int IsValidPortalUser(string p_u, string p_p) {
            string str = DB_Connection.My_Decryption_2(p_p);
            int item = 0;
            try
            {
                if (DB_Connection.conn.State == 0)
                {
                    DB_Connection.conn.Open();
                }
                DB_Connection.DBCmd = new IfxCommand();
                DB_Connection.DBCmd.Connection = DB_Connection.conn;
                DB_Connection.DBCmd.CommandText = "SELECT nvl(emp_num,0) FROM htoemp WHERE username = ? AND DECRYPT_CHAR(password, '78dfdf') = ? ";
                DB_Connection.DBCmd.Parameters.Add("user_name", p_u);
                DB_Connection.DBCmd.Parameters.Add("password", str);
                IfxDataReader ifxDataReaders = DB_Connection.DBCmd.ExecuteReader();
                if (ifxDataReaders.Read())
                {
                    item = (int)ifxDataReaders[0];
                }
                ifxDataReaders.Close();
            }
            catch (ApplicationException applicationException)
            {
            }
            DB_Connection.conn.Close();
            return item;
        }

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

无论您在using块内使用哪个库,都需要构建一个新的Connection对象。在using块之外使用数据库连接对象是危险的,因为当发生某些异常时它可能导致打开连接。

此外,在Web应用程序中,您不能拥有类范围的私有或静态连接对象,因为这会导致多个线程尝试使用相同的连接对象问题。静态连接只能在Windows应用程序中使用,而不能在Web应用程序中使用。

处理数据库连接的正确模式是:

try
{
   Open Connection
   Do the work
   Close connection
}
catch
{
   Handle error.
}
finally
{
    try
    {
        Close Connection.
    }
    catch 
    {
        Do nothing.
    }
}

或者您可以使用using语法。

using (var connection = OpenConnection())
{
   connection.close();
}

可帮助?