Commit 730b2e3
committed
Locking phase 1b — row-level Shared / Update / Exclusive data locks with the full SQL Server 8-mode matrix (Sch-S / Sch-M / IS / IX / SIX / S / U / X),
SET TRANSACTION ISOLATION LEVEL session-state with semantic effect, row-lock escalation at 5000 per-tx per-table, and the UPDLOCK / XLOCK / READPAST / TABLOCK / TABLOCKX hint surface. LockMode extended to 8 entries; LockManager.IsCompatible rewritten as the standard SQL Server matrix (IS × {IS, IX, SIX, S, U} compatible / IS × X conflict; IX × IX compatible / IX × {S, U, SIX} conflict; SIX × IS only; S × {S, U, IS} compatible / S × {U, X, IX, SIX} … wait the real matrix has S × U compatible). New HeapTable.RowLocks (ConcurrentDictionary<(int pageIndex, int slotIndex), LockResource>) holds lazily-interned per-row resources keyed by RID; new HeapTable.TableDataLock is the table-level data lock (IS/IX/SIX/S/U/X), distinct from the inherited SchemaObject.SchemaLock which still carries only Sch-S / Sch-M. New DataLockPlan struct (returned by the refactored BatchContext.AcquireDataLockIfApplicable) encodes the per-row strategy for the caller to execute during iteration / mutation: row mode (S / U / X / null-probe-only / null-skip-noLock), tx-scoping, READPAST flag. Heap.Insert API change from void to (int PageIndex, int SlotIndex) return so DML sites can attach row-X to the freshly-inserted row's RID; 11 call sites updated (4 DML write sites capture and acquire row-X tx-scoped; 7 internal / history-table / constraint-cascade / system-table-init / TVP-clone / SELECT-INTO sites discard with _ =). BatchContext.AcquireRowLockTxScoped(table, page, slot, mode) is the central row-level acquire that bumps SimulatedDbTransaction.RowLockCountsByTable and triggers escalation when the per-table count crosses RowLockEscalationThreshold (5000, matching real SQL Server's documented default) — EscalateToTableX acquires the table-X first (may throw on timeout / cycle, leaves partial state consistent), then releases every row-lock entry on the escalated table from tx.HeldLocks, then marks the table in tx.EscalatedTables so future per-row acquires short-circuit. BatchContext.TouchRowForRead(table, page, slot, plan) is the reader's per-row helper: NoLockReader = no probe; non-null RowMode = acquire that mode tx-scoped (for HOLDLOCK / RR / UPDLOCK / XLOCK); else probe-only (RC default — LockManager.HasIncompatibleHolderOtherThan + transient acquire-release row-S on conflict matching real SQL Server's "wait for committed row" pattern). BatchContext.WrapWithRowConflictChecks wraps Heap.EnumerateRowsWithAddress into the FromSource.Rows enumerable so each yielded row flows through the touch helper; READPAST returns false from TouchRowForRead so the wrapper skips the row instead of yielding it. Selection.TableHintInfo gains UpdLock / XLock / ReadPast / TabLock / TabLockX / Serializable / Repeatable flags (split from phase-1a's combined HoldLock — HOLDLOCK / SERIALIZABLE = Serializable, REPEATABLEREAD = Repeatable since they have different semantics: SER prevents phantoms via table-S, RR doesn't). SimulatedDbConnection.SessionIsolationLevel (default ReadCommitted) gets wired through SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SNAPSHOT | SERIALIZABLE } in Simulation.Set.cs — RU promotes every read to dirty-read (NOLOCK-equivalent), RC default, RR retains row-S tx-scoped per read row, SER takes table-S tx-scoped at scan start for phantom prevention at table granularity (the simulator's approximation of SQL Server's key-range locks since no index range structure is modeled), SNAPSHOT parses-and-discards (MVCC is phase 3). The hint-driven AcquireDataLockIfApplicable dispatch table: TABLOCKX → table-X (skip row); TABLOCK reader → table-S (tx-scoped under HOLDLOCK / RR / SER / session RR / SER); TABLOCK writer → table-X; writer default → table-IX + per-row X tx-scoped; reader XLOCK → table-IX + per-row X tx-scoped; reader UPDLOCK → table-IX + per-row U tx-scoped; reader SER hint / session SER → table-S tx-scoped (no per-row); reader RC default → table-IS + per-row probe-only; reader RR hint / session RR → table-IS + per-row S tx-scoped; table variables / local temp tables / system tables bypass everything. **Practical effect**: the phase-1a "stronger than RC" quirk is gone — a SELECT no longer blocks on any X-locked row in the table, only on the specific row being touched. Two writers on disjoint rows of the same table now proceed in parallel through table-IX + disjoint row-X. The phase-1a HoldLockHint_KeepsSUntilCommit test (which expected the HOLDLOCK hint to block a phantom-creating INSERT) now passes for the right reason: HOLDLOCK takes table-S tx-scoped instead of row-S, matching real SQL Server's documented "HOLDLOCK = SERIALIZABLE" equivalence. 6 new internal LockResourceTests (U-mode compatibility, IS-orthogonality, IX-conflict, SIX-only-with-IS, escalation past threshold) plus 9 new user-facing LockingTests (row-level RC disjoint-row independence, READPAST skip-blocked-row, UPDLOCK U-U conflict, XLOCK reader-blocking, TABLOCKX blocking, SERIALIZABLE phantom-prevention, REPEATABLE READ row-S blocks UPDATE but allows INSERT, READ UNCOMMITTED dirty read, two-writer disjoint-row parallel proceed). Phase-1a TransactionScopedX_ReleasedAtCommit / _AtRollback internal tests updated to check TableDataLock (phase 1b's table-data lock) instead of SchemaLock (which only carries Sch-S / Sch-M now). docs/claude/locking.md rewritten as the unified phases-0+1a+1b doc with the 8-mode matrix, the granularity-dispatch tables, the row-lock storage / escalation / acquisition-site documentation, and a phase-1b gaps section calling out key-range locks (table-S approximation), page-level (PAGLOCK no-op), hint-conflict detection (Msg 1047 / 1065 / 1069), sys.dm_tran_locks (phase 2), @@LOCK_TIMEOUT scalar (phase 2), SNAPSHOT / RCSI (phase 3), alias-form UPDATE / DELETE row-X (deferred), ALTER PROCEDURE / ALTER TRIGGER / ALTER SEQUENCE Sch-M wiring (deferred), row-lock cleanup on DELETE (intentional leak matching heap slot-leak pattern), history-table / cascade-FK / OUTPUT-INTO row-X (deferred). CLAUDE.md transactions bullet rewritten to describe phase-1b row-level RC + isolation-level semantic effect; Feature-reference entry expanded with all the new lock APIs; "Not modeled" rewritten — the "stronger than RC" quirk is gone, phase 1b ships substantially more than phase 1a; SimulatedDbConnection session-state listing adds SessionIsolationLevel.1 parent 37f5bfe commit 730b2e3
26 files changed
Lines changed: 1340 additions & 271 deletions
File tree
- SqlServerSimulator.Tests.Internal
- Storage
- SqlServerSimulator.Tests
- SqlServerSimulator
- Parser
- Simulation
- Storage
- docs/claude
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
208 | | - | |
209 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
210 | 211 | | |
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
214 | | - | |
| 215 | + | |
215 | 216 | | |
216 | | - | |
| 217 | + | |
217 | 218 | | |
218 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
219 | 222 | | |
220 | 223 | | |
221 | 224 | | |
| |||
225 | 228 | | |
226 | 229 | | |
227 | 230 | | |
228 | | - | |
| 231 | + | |
229 | 232 | | |
230 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
231 | 236 | | |
232 | 237 | | |
233 | 238 | | |
| |||
285 | 290 | | |
286 | 291 | | |
287 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
288 | 415 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
| 36 | + | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
98 | | - | |
99 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| |||
0 commit comments