0

我从未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;
        }

如何解决这个问题?

4

1 回答 1

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();
}

有帮助吗?

于 2012-07-18T10:35:17.130 回答