We have a job that must process each new record in a SQL Server table. This table has a column Created
with a constraint which defaults this column's value to GETUTCDATE()
. The job's procedure is as follows:
- The job queries for all records whose
Created
value is greater than or equal to the last seenCreated
value - The job processes all returned records
- The job updates its last seen
Created
value to the highestCreated
value it saw in the records.
This seems to work pretty well, however, we have noticed that the job sometimes skips records. We believe this may be because of a scenario like:
- Transaction A starts to insert a new record. SQL Server defaults the
Created
value to1
- Transaction B starts to insert a new record. SQL Server defaults the
Created
value to2
- Transaction B commits
- Job processes the table and sees the record created by transaction B. The job updates its last seen
Created
value to2
. - Transaction A commits
- Job processes the table but "skips" the record created by transaction A as its
Created
value is smaller than the last seenCreated
value.
Is such a scenario possible? Will a read operation not block until the pending insert transactions are completed?