@@ -4,7 +4,62 @@ Fixes are prioritised by the severity rankings in [03-issues.md](03-issues.md).
44
55---
66
7- ## Fix 1 — Implement ` ChannelDbConnectionPool.ReplaceConnection ` (Issue A)
7+ ## Fix 1 — Verify and correct database context after session recovery (Issue G) ✅ IMPLEMENTED
8+
9+ ### Goal
10+
11+ After a successful session recovery, ensure both the client and server are on the database the
12+ session was using before the connection dropped.
13+
14+ ### Root cause
15+
16+ ` CompleteLogin() ` unconditionally trusts the server's ` ENV_CHANGE(ENV_DATABASE) ` response after
17+ session recovery. If the server fails to restore the database (sends the initial catalog, or omits
18+ the database ` ENV_CHANGE ` entirely), ` CurrentDatabase ` silently ends up wrong.
19+
20+ ### Approach
21+
22+ In ` CompleteLogin() ` , after the server has acknowledged session recovery and encryption is verified:
23+
24+ 1 . Read the expected database from ` _recoverySessionData._database ` (the database at disconnect
25+ time), falling back to ` _recoverySessionData._initialDatabase ` if ` _database ` is null (meaning
26+ the database was never changed from the initial login).
27+ 2 . Null ` _recoverySessionData ` (as before).
28+ 3 . Compare the expected database against ` CurrentDatabase ` (which was set by the server's
29+ ` ENV_CHANGE ` during login).
30+ 4 . If they differ, issue a ` USE [database] ` command over the wire to force the server to the
31+ correct database. This ensures both client and server agree.
32+ 5 . Set ` CurrentDatabase ` to the recovered database as a final safety net.
33+
34+ When the databases already match (the normal case with a well-behaved server), no ` USE ` is sent —
35+ zero overhead.
36+
37+ ### Files changed
38+
39+ | File | Change |
40+ | ---- | ------ |
41+ | ` SqlConnectionInternal.cs ` | Added database context verification and ` USE ` correction in ` CompleteLogin() ` after session recovery |
42+
43+ ### Tests added
44+
45+ | Test | Scenario |
46+ | ---- | -------- |
47+ | ` UseDatabase_ProperRecovery_DatabaseContextPreservedAfterReconnect ` | Server properly restores DB via session recovery — baseline |
48+ | ` ChangeDatabase_ProperRecovery_DatabaseContextPreservedAfterReconnect ` | Same, via ` ChangeDatabase() ` |
49+ | ` UseDatabase_ProperRecovery_Pooled_DatabaseContextPreservedAfterReconnect ` | Same, with pooling enabled |
50+ | ` UseDatabase_BuggyRecovery_DatabaseContextPreservedAfterReconnect ` | Server sends wrong DB in ` ENV_CHANGE ` — fix issues ` USE ` to correct |
51+ | ` ChangeDatabase_BuggyRecovery_DatabaseContextPreservedAfterReconnect ` | Same, via ` ChangeDatabase() ` |
52+ | ` UseDatabase_OmittedEnvChange_DatabaseContextPreservedAfterReconnect ` | Server omits DB ` ENV_CHANGE ` entirely — fix issues ` USE ` to correct |
53+ | ` ChangeDatabase_OmittedEnvChange_DatabaseContextPreservedAfterReconnect ` | Same, via ` ChangeDatabase() ` |
54+ | ` UseDatabase_ConnectionDropped_NoRetry_ThrowsOnNextCommand ` | ` ConnectRetryCount=0 ` — no recovery, error surfaces |
55+
56+ ### Verification
57+
58+ All 10 tests pass. Full unit test suite (631 tests) shows no regressions.
59+
60+ ---
61+
62+ ## Fix 2 — Implement ` ChannelDbConnectionPool.ReplaceConnection ` (Issue A)
863
964### Goal
1065
@@ -38,7 +93,7 @@ Implement `ReplaceConnection` in `ChannelDbConnectionPool` to mirror the behavio
3893
3994---
4095
41- ## Fix 2 — Make ` CurrentSessionData ` snapshot atomic (Issue B)
96+ ## Fix 3 — Make ` CurrentSessionData ` snapshot atomic (Issue B)
4297
4398### Goal
4499
@@ -88,7 +143,7 @@ different thread) don't affect the recovery data. This is safer but allocates.
88143
89144---
90145
91- ## Fix 3 — Document ` USE [db] ` vs ` ChangeDatabase ` resilience difference (Issue F)
146+ ## Fix 4 — Document ` USE [db] ` vs ` ChangeDatabase ` resilience difference (Issue F)
92147
93148### Goal
94149
@@ -114,7 +169,7 @@ retry.
114169
115170---
116171
117- ## Fix 4 (Optional) — Warn when ` USE [db] ` is detected without session recovery (Issue F)
172+ ## Fix 5 (Optional) — Warn when ` USE [db] ` is detected without session recovery (Issue F)
118173
119174### Goal
120175
0 commit comments