我有一个连接类来处理我的 Informix 数据库查询。它有两个功能;一种用于执行简单查询,一种用于返回数据表。间歇性地(尤其是当我让会话静置一会儿时,例如十分钟时)在请求新信息时,我会在 conn.open 命令上收到一个废弃的互斥锁错误。
这是有问题的代码:
public DataTable CallDtQuery(string query)
{
DataTable dt = new DataTable();
using (IBM.Data.Informix.IfxConnection conn = new
IBM.Data.Informix.IfxConnection(sqlConnection))
{
try
{
IBM.Data.Informix.IfxDataAdapter adapter = new IfxDataAdapter();
adapter.SelectCommand = new IBM.Data.Informix.IfxCommand(query, conn);
conn.Open(); //Error location.
adapter.Fill(dt);
conn.Close();
}
catch (IBM.Data.Informix.IfxException ex)
{
LogError(ex, query);
SendErrorEmail(ex, query);
DisplayError();
}
}
return dt;
}
此外,这里是简单查询函数,它是应用程序中唯一连接到数据库的其他函数:
public string CallSimpleQuery(string query, string command)
{
string result = "";
using (IBM.Data.Informix.IfxConnection conn = new
IBM.Data.Informix.IfxConnection(sqlConnection))
{
try
{
IBM.Data.Informix.IfxDataAdapter adapter = new IfxDataAdapter();
conn.Open();
switch (command)
{
case "UPDATE":
adapter.UpdateCommand = new IBM.Data.Informix.IfxCommand(query, conn);
result = adapter.UpdateCommand.ExecuteNonQuery().ToString();
break;
case "DELETE":
adapter.DeleteCommand = new IBM.Data.Informix.IfxCommand(query, conn);
result = adapter.DeleteCommand.ExecuteNonQuery().ToString();
break;
case "SELECT":
adapter.SelectCommand = new IBM.Data.Informix.IfxCommand(query, conn);
result = adapter.SelectCommand.ExecuteScalar().ToString();
break;
case "INSERT":
adapter.InsertCommand = new IBM.Data.Informix.IfxCommand(query, conn);
result = adapter.InsertCommand.ExecuteNonQuery().ToString();
break;
}
conn.Close();
}
catch (IBM.Data.Informix.IfxException ex)
{
LogError(ex, query);
SendErrorEmail(ex, query);
DisplayError();
}
}
return result;
}
这是生成的错误:
Error Message = The wait completed due to an abandoned mutex.
Message Source:
mscorlib
=============================
Message Target:
Boolean WaitOne(Int64, Boolean)
=============================
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.IfxConnPoolManager.GetPool(IfxConnSettings key)
at IBM.Data.Informix.IfxConnPoolManager.Open(IfxConnection connection)
at IBM.Data.Informix.IfxConnection.Open()
at XXX.Connections.CallDtQuery(String query) in d:\Inetpub\wwwroot\intranet\CWSheet-test2\App_Code\Connections.cs:line 75
at XXX.details.Page_Load(Object sender, EventArgs e) in d:\Inetpub\wwwroot\intranet\CWSheet-test2\Details.aspx.cs:line 29
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)
当错误发生时,它不会进入 catch 块并运行 Log、Send 和 Display 错误函数,而是放入 global.asax 上的 Application_Error 块。由于所有内容都包含在 try/catch 块中,因此我不确定是什么原因造成的。此外,无论出于何种原因,应用程序有时会在 CallDtQuery 上挂起。我将在表单视图中翻阅记录页面,它会突然挂在 CallDtQuery 请求上。有时它可能会在一两分钟后通过,有时它会无限期挂起,直到应用程序在 30 分钟后超时。
我一直在阅读一些关于互斥锁的文章,但以前从未使用过它。ASP.NET 应用程序会自动生成正在使用的任何互斥锁。考虑到这一点,我不确定如何解决或解决此问题。有什么建议么?