我们可以使用单个数据库连接同时从多个线程执行多个数据库选择语句吗?

时间:2022-06-11 16:56:25

I want to use single database connection from multiple threads to read (or to execute only select statements) in MS SQL Server in simultaneously. Is it possible to execute all these select statements in simultaneously from different threads.

我想使用来自多个线程的单个数据库连接来同时在MS SQL Server中读取(或仅执行select语句)。是否可以从不同的线程同时执行所有这些select语句。

I m using MS SQL Server from C++ in Linux environment. I need to create Database connection pools for reading and writing separately. So i want to know is there a possibility of sharing a same connection between threads to read only.

我在Linux环境中使用C ++中的MS SQL Server。我需要创建数据库连接池以分别进行读写。所以我想知道是否有可能在线程之间共享相同的连接以进行只读。

The select statements may return multiple rows (more than one row or result set). Will this be a problem?

select语句可能返回多行(多行或结果集)。这会是一个问题吗?

3 个解决方案

#1


4  

Yes there will be a problem. Only one command can be executed at a time. But you'll be fine using multiple connections, connection pooling works great for SQL server.

是的,会有问题。一次只能执行一个命令。但是你可以使用多个连接,连接池对SQL服务器很有用。

#2


4  

Don't use the same connection across threads. Only one command can be executed per connection. Create a connection for each thread. I'd suggest making a helper class to make this easier for you.

不要跨线程使用相同的连接。每个连接只能执行一个命令。为每个线程创建连接。我建议你做一个辅助课,让你更容易。

#3


0  

I want to use single database connection from multiple threads to read (or to execute only select statements) in MS SQL Server in simultaneously

我想使用来自多个线程的单个数据库连接来同时在MS SQL Server中读取(或仅执行select语句)

You should start reading the documentation and not start having funny ideas that whas you want matters here.

你应该开始阅读文档而不是开始有你想要的有趣想法。

Yes, you CAN do that (MARS is the topic- read it up) but one connection can alwayso nly have one transaction context so it isa good approach to ahvem ultiple selects in one transaction (insert, couple of upserts etc.) but bad as a generic approach for progrmaming database connections.

是的,你可以这样做(MARS是主题 - 读它),但是一个连接也可以只有一个事务上下文,所以它是一个很好的方法来在一个事务中多次选择(插入,几个upsert等)但是很糟糕用于编程数据库连接的通用方法。

Follow the recipes which mean iopen a conenction when you need it, close it when done and dont be afraid to run mtultiple connections.

按照食谱意味着在需要时打开一个连接,完成后关闭它并且不要害怕运行多个连接。

#1


4  

Yes there will be a problem. Only one command can be executed at a time. But you'll be fine using multiple connections, connection pooling works great for SQL server.

是的,会有问题。一次只能执行一个命令。但是你可以使用多个连接,连接池对SQL服务器很有用。

#2


4  

Don't use the same connection across threads. Only one command can be executed per connection. Create a connection for each thread. I'd suggest making a helper class to make this easier for you.

不要跨线程使用相同的连接。每个连接只能执行一个命令。为每个线程创建连接。我建议你做一个辅助课,让你更容易。

#3


0  

I want to use single database connection from multiple threads to read (or to execute only select statements) in MS SQL Server in simultaneously

我想使用来自多个线程的单个数据库连接来同时在MS SQL Server中读取(或仅执行select语句)

You should start reading the documentation and not start having funny ideas that whas you want matters here.

你应该开始阅读文档而不是开始有你想要的有趣想法。

Yes, you CAN do that (MARS is the topic- read it up) but one connection can alwayso nly have one transaction context so it isa good approach to ahvem ultiple selects in one transaction (insert, couple of upserts etc.) but bad as a generic approach for progrmaming database connections.

是的,你可以这样做(MARS是主题 - 读它),但是一个连接也可以只有一个事务上下文,所以它是一个很好的方法来在一个事务中多次选择(插入,几个upsert等)但是很糟糕用于编程数据库连接的通用方法。

Follow the recipes which mean iopen a conenction when you need it, close it when done and dont be afraid to run mtultiple connections.

按照食谱意味着在需要时打开一个连接,完成后关闭它并且不要害怕运行多个连接。