开发者

Autocommit and multiple SqlConnections

开发者 https://www.devze.com 2022-12-27 05:44 出处:网络
Is an insert with autocommit guaranteed to be immediately visible to any/every SqlConnection on the database, or only on it\'s own SqlConnection?

Is an insert with autocommit guaranteed to be immediately visible to any/every SqlConnection on the database, or only on it's own SqlConnection?

I have a procedure with 3 SqlConnections. Connection One is associated with a 开发者_StackOverflowSqlDataReader which is driving a loop. Withing the loop, Connection Two is used to get data for test, and Connection Three is used to insert rows in a table. The insert autocommits.

We have a situation where the test (using Connection Two data) is failing. One possible explanation is that Connection Two sometimes does not see the row inserted by Connection Three on the previous pass through the loop. Is this possible?

I don't see a problem with combining Two and Three, and I'm going to do that, but I don't have any way to force this error to occur, and I'd like to know if this explanation is even possible.


Yes, auto-committed inserts are the same as any other committed inserts and will be visible on other connections.

However, inserting rows won't change the rows returned by an already-existing SqlDataReader, since the SqlDataReader object encapsulates a record set - the results of a query - and not the query itself.

I'm not sure what your "Connection Two" is doing, but if it's depending on the results of the INSERT in C3 being visible to the SqlDataReader, then it will definitely fail for this reason.


Is this what you're doing?

  1. You use open a SQLDataReader which will get the data and then loop through it.
  2. During this loop, you insert a record that you then expect via the select statement used in the reader.
  3. You don't see it in the loop, as the data has already been selected.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号