使用/打开和关闭数据库连接的线程数 [英] Number of threads to use/opening and closing DB connection

查看:62
本文介绍了使用/打开和关闭数据库连接的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个由两部分组成的问题,第1部分:我编写了一个多线程生产者消费者队列,该队列接收实时股票市场更新并将数据传递到数据库中进行处理.我目前已将数据库操作的线程数设置为4(机器处理器数量).我想有一种更系统的方法来确定线程数以获得最高效率吗?我的应用程序似乎显示了延迟.而且我想知道是否可以增加线程数来解决延迟问题.

另外,我想知道是否应该在每个数据库方法调用上打开和关闭数据库连接?我问的原因是因为我要打数据库.每秒10次.

预先感谢,
-Dee

Hi,

I have a two part question, part 1: I''ve written a multithread producer consumer queue that''s receiving real-time stock market updates and passing the data on to a database for processing. I''ve current set my thread count on the database operation to 4 (number of machine processors). I''d like to is there a more systematic way to determine the number of threads in order to gain the most efficiency? My application seems to displaying latency. And I was wondering if I increased my thread count would that resolve my latency issue.

Also, I wanted to know should I be opening and closing my database connection on each database method call? The reason why I ask is because I''m hitting the DB approx. 10 times per second.

Thanks in advance,
-Dee

推荐答案

您最好维护与数据库的单个持久性开放连接,并始终使用该连接.打开新连接非常昂贵,每秒执行10次可能会导致速度降低.根据所使用的数据库,您可能无法在不同的线程中使用相同的连接(请参阅文档),尤其是在编写数据时,因此可能需要一个数据库查询队列(其中显然会创建线程同步点.)
You may be better off maintaining a single, persistent, open connection to the database, and using that all the time. Opening a new connection is quite expensive and doing that 10 times per second may be causing some slowdown. Depending on the database you''re using, you may not be able to use the same connection in different threads (check the documentation), particularly if you''re writing data, so you might need to have a database query queue (which would obviously create a thread synchronisation point).


这篇关于使用/打开和关闭数据库连接的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆