IfxConnection 和线程可以相处吗?
我们有一个使用 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 位)上进行测试的。
We have an application using the IBM Informix driver. Whenever we try to open connections in parallel, we start getting some really weird errors.
This is the smallest reproduction code I could come up with:
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();
}
Depending on which machine we run it on, we had to play a little with the Count
value to make it fail, but 10 seems to reproduce the error consistently.
Of course this is not production code, nor how we handle threading, but it highlights the issue without introducting any other variables.
This is the exception stack trace:
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 version is 3.00.06000.2.
This was tested on Windows 7 (32 and 64 bits) and 2008 (64 bits).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决了这个问题,在连接字符串上添加了“Pooling=false”。
I solved this issue adding a "Pooling=false" on the connection string.
我在内部询问了这个问题,并得到了有关 IBM Informix ClientSDK .NET 提供程序的以下反馈:
由于我没有制定答案,因此我将其设为社区 Wiki。
I asked about this internally, and got the following feedback about the IBM Informix ClientSDK .NET provider:
Since I didn't develop the answer, I've made it Community Wiki.