views:

36

answers:

2

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 SqlDataReader 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.

+1  A: 

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.

Aaronaught
A: 

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.
Paddy
Let me put it like this:I have some transactions to be processed. There are multiple transactions for each account. There is an annual limit, so transactions are not processed once the limit is met.I loop through the transactions using Connection One and a SqlDataReader. In the loop, I use a select statement on Connection Two to find the current total. If the total is still less than the limit, I use Connection Three to insert a row.If it's the same account, the Total brought back by the Select should (and usually does) include the row inserted on the previous pass through the loop.
SeaDrive
@SeaDrive - In which case, are you sure your insert always fires as expected?
Paddy
The rows are always there eventually. Taking Aaronaught's first sentence as correct, I have to look elsewhere for the error. I have an alternate hypothesis having to do with the function bringing back the running total.
SeaDrive