简体   繁体   English

我应该在多线程程序中使用连接池吗?

[英]Should I use connection-pooling in multi-threaded program?

I am using multiple threads to insert insert records in different tables. 我正在使用多个线程在不同的表中插入插入记录。 In addition, I am using batch processing for the insertion of records to improve the efficiency. 另外,我正在使用批处理来插入记录以提高效率。

Note: Number of records to be inserted are in millions. 注意:要插入的记录数以百万为单位。

My question is should I use connection pooling in this multi-threaded environment? 我的问题是我应该在这种多线程环境中使用连接池吗?

My Concern: 我的顾虑:

Each threads gonna run for quite sometime to perform the database operation. 每个线程将运行相当一段时间以执行数据库操作。 So, if the size of my connection pool is 2 and number of threads are 4,then at a given moment only 2 threads are gonna run. 因此,如果我的连接池的大小为2,线程数为4,那么在给定的时刻只有2个线程可以运行。 Consequently, other 2 threads gonna sit ideal for a long time to get the connection, as the db operations for million records are time consuming. 因此,其他2个线程将很长一段时间都非常适合建立连接,因为百万条记录的数据库操作非常耗时。 Moreover, such connection-pooling will hinder the purpose of using multiple threads. 而且,这种连接池将阻碍使用多个线程的目的。

Using a connection pool in a batch is a matter of convenciency. 批量使用连接池很麻烦。 It will help you limit the amount of open connections, abandoned time, close connections if you forget to close them verify if the connection is open etc. 它将帮助您限制打开的连接数,放弃的时间,关闭连接(如果您忘记关闭它们),验证连接是否打开等。

Check out the Plain Ol' Java example here 在此处查看Plain Ol'Java示例

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

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