简体   繁体   English

实现连接池的正确方法

[英]Correct way to implement a connection pool

I'm trying to write a multithreading program that connects to a MySQL database and processes the returned set for a query (which has thousands of rows). 我正在尝试编写一个multithreading program ,该multithreading program连接到MySQL database并处理查询的返回集(具有数千行)。 The problem is that I have implemented the connection pool and I get every thread to open the connection to the database and get the resulting set. 问题是我已经实现了连接池,并且我得到了每个线程来打开与数据库的连接并获得结果集。 But I don't understand which is the advantage of using connection-pooling if retrieving that big set takes such a lot of time. 但是我不明白,如果检索这么大的数据集需要花费大量时间,那么使用连接池的好处是什么。 It wouldn't be better if I get the whole set with only one connection (without using pooling) and then I use thread pooling to process it? 如果仅通过一个连接(不使用池)获得整个集合,然后使用thread pooling来处理它,那会更好吗? Or is there a way that every thread takes the next row of the resulting set? 还是有办法使每个线程都使用结果集的下一行?

If you have a limited number of threads, I would have a connection per thread. 如果线程数量有限,则每个线程都有一个连接。

A connection pool is more efficient if the number of threads which could use a connection is too high and those thread use the connections a relatively low percentage of the time. 如果可以使用连接的线程数量过多,而那些线程使用连接的时间百分比相对较低,则连接池效率更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM