【发布时间】:2010-03-11 16:22:44
【问题描述】:
我们有一个使用 IBM Informix 驱动程序的应用程序。每当我们尝试并行打开连接时,我们就会开始遇到一些非常奇怪的错误。
这是我能想到的最小的复制代码:
const int Count = 10;
const string ConnectionString = "Host=the_host;Service=the_service;Server=the_server;Database=the_database;User ID=the_user_id;Password=the_password";
static void Main()
{
var threads = new Thread[Count];
for (var i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(
number =>
{
using (var conn = new IfxConnection(ConnectionString))
{
Console.WriteLine("Opening connection {0}... ", number);
try
{
conn.Open();
Console.WriteLine("Opened connection {0}", number);
var setLockCommand = conn.CreateCommand();
setLockCommand.CommandText = "set lock mode to wait 10;";
setLockCommand.ExecuteNonQuery();
Console.WriteLine("Releasing connection {0}", number);
}
catch (IfxException ex)
{
Console.WriteLine("Failed opening connection {0}: {1}", number, ex);
}
}
});
threads[i].Start(i);
}
foreach (var thread in threads)
thread.Join();
}
根据我们在哪台机器上运行它,我们不得不稍微调整一下Count 值以使其失败,但 10 似乎始终如一地重现错误。
当然,这不是生产代码,也不是我们处理线程的方式,但它在没有引入任何其他变量的情况下突出了问题。
这是异常堆栈跟踪:
IBM.Data.Informix.IfxException: ERROR [HY000] [Informix .NET provider]General error.
at IBM.Data.Informix.IfxConnection.GetConnectAttr(SQL_ATTR attribute, HANDLER handler)
at IBM.Data.Informix.IfxConnection.ConnectionIsAlive()
at IBM.Data.Informix.IfxConnectionPool.BindNodeToConnection(IfxConnPoolNode poolNode, IfxConnection connection, ConnectionPoolType ConnPoolType)
at IBM.Data.Informix.IfxConnectionPool.Open(IfxConnection connection)
at IBM.Data.Informix.IfxConnPoolManager.Open(IfxConnection connection)
at IBM.Data.Informix.IfxConnection.Open()
IBM.Data.Informix.dll 版本为 3.00.06000.2。
这是在 Windows 7(32 位和 64 位)和 2008(64 位)上测试的。
【问题讨论】:
标签: .net multithreading informix