Question

I have never used mutex in my application and i don't now what it does, but my web application throw the following exception and i don't know how to handle it :

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

How to fix this problem ?

Was it helpful?

Solution

You always need to construct a new Connection object, no matter what library you are using inside a using block. Using database Connection object outside using block is dangerous as it can lead to open connection when some exception occurs.

Moreover, on a web application, you cannot have a class wide private or static connection object because that will lead to multiple threads trying to use the same connection object problem. Static connection can only be used in Windows applications, not in Web Applications.

The right pattern for dealing with database connection is:

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

Or you can just use the using syntax.

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

Helps?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top