线程卡在 MySqlCommand.ExecuteNonQuery() 中
我想并行调用方法。在这种方法中,我计算一些值(非常快)并将该计算的值写入数据库表中。
因此我使用线程池
ThreadPool.QueueUserWorkItem((state) => {
method();
});
经过一段时间的计算(不确定,发生这种情况的时间各不相同,有时在几分钟后,有时在几个小时后)线程池已满,但不是与 method() 的线程,而是与线程我的 mysql 类调用 MySqlCommand.ExecuteNonQuery 。
有谁知道如何避免这种情况?在 mysql 类的文档中,我发现您无法终止正在执行的查询,但如何避免此类问题?
I want to make the calls to a method parallel. In this method I calculate some value (very fast) and write the value of this calculation into a database table.
Therefore I use a Threadpool
ThreadPool.QueueUserWorkItem((state) => {
method();
});
After a while of calculation (not deterministic, the time when this happens varies, sometimes after a few minutes, sometimes after some hours) the Threadpool is full, but not with the thread of method() but with the thread of my mysql class that calls MySqlCommand.ExecuteNonQuery
.
Does anyone have an idea how to avoid this? In the documentation of the mysql classes I found out that you cannot terminate an executing query but how to avoid such problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否需要在计算出所有值后将所有值写入数据库,或者您可以等到有一些值,然后在插入大量值时进行批量更新吗?
如果所有线程尝试同时写入同一个表,您可能会遇到锁定问题。所以尝试将数字存储在本地,然后一大批提交
Do you need to write all values to the DB once they are calculated, or can you wait till you have some, and do a batch update where you insert a lot of them?
You could be running into a locking issue here, if all threads try to write to the same table at the same time. So try to store the numbers locally and then submit them in one big batch