0

当我同时有多个DBDataReaders读取数据时,我收到以下错误:

There is already an open DataReader associated with this Connection which must be
closed first

我在我的配置中启用了 ConnectionPooling,所以我不明白为什么会收到此错误。由于我当前的连接已经在使用中,它不应该创建一个新连接吗?

我知道设置MultipleActiveResultSets为 true 可以解决问题,但我仍在尝试了解问题存在的原因

4

1 回答 1

1

Connection pooling does not do what you think it does.

If you do something like this

var connection = new SqlConnection(connectionString);
connection.Open();
var command = connection.CreateCommand();
command.CommandText = // some query
var reader = command.ExecuteReader();
var anotherCommand = connection.CreateCommand();
anotherCommand.CommandText = // another query
var anotherReader = anotherCommand.ExecuteReader();

then all of this will happen on one connection, whether or not you have connection pooling.

Connection pooling just keeps a cache of connections that you can draw from every time that you create a new connection (new SqlConnection) and open it (SqlConnectinon.Open). When you close a connection, it returns to the pool to be reused. But one open SqlConnection object corresponds to one connection from the pool. Period.

于 2011-09-25T03:11:41.220 回答