Skip to content

Commit b1201e2

Browse files
thorajclaude
andcommitted
docs: Clarify transaction coordination mechanism
Update Step 1 to explain that: - Each UseSqlServer(connectionString) creates a NEW connection object - Matching connection strings signal the adapter to attempt coordination - Adapter uses UseTransaction() to coordinate separate connection objects - Transaction sharing works because SQL Server supports coordination at DB level This addresses the misconception that same connection string = same connection object. Instead, the adapter coordinates SEPARATE connection objects via UseTransaction() when the connection strings match. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent c84f6d4 commit b1201e2

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

MULTI_CONTEXT_USAGE_GUIDE.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,43 @@ Multi-context support allows you to store different Casbin policy types in separ
2020

2121
Create separate `CasbinDbContext` instances for different storage locations.
2222

23-
**⚠️ IMPORTANT for transaction guarantees:** To ensure atomic operations across contexts:
24-
1. All contexts must use the **same connection string** (same database)
25-
2. The database must support `UseTransaction()` to share a physical connection object
26-
3. **Same connection string alone is not enough** - the database must be able to coordinate transactions
23+
**⚠️ CRITICAL - Transaction Guarantees:**
24+
25+
The adapter detects transaction compatibility by comparing connection strings. If connection strings match, it will attempt to use `UseTransaction()` to coordinate transactions. However:
26+
27+
- **Matching connection strings** tell the adapter to TRY sharing transactions
28+
- **Actual transaction sharing** depends on the database's ability to coordinate via `UseTransaction()`
29+
- SQL Server, PostgreSQL, MySQL can share transactions when contexts connect to the **same database**
30+
- SQLite can share transactions only when contexts use the **same physical file**
2731

2832
**Example: SQL Server with different schemas**
2933

3034
```csharp
3135
using Microsoft.EntityFrameworkCore;
3236
using Casbin.Persist.Adapter.EFCore;
3337

34-
// Define connection string once - REQUIRED for atomic transactions
38+
// Define connection string once
3539
string connectionString = "Server=localhost;Database=CasbinDB;Trusted_Connection=True;";
3640

3741
// Policy context - "policies" schema
3842
var policyContext = new CasbinDbContext<int>(
3943
new DbContextOptionsBuilder<CasbinDbContext<int>>()
40-
.UseSqlServer(connectionString) // Same connection string = atomic transactions
44+
.UseSqlServer(connectionString)
4145
.Options,
4246
schemaName: "policies");
4347
policyContext.Database.EnsureCreated();
4448

4549
// Grouping context - "groupings" schema
4650
var groupingContext = new CasbinDbContext<int>(
4751
new DbContextOptionsBuilder<CasbinDbContext<int>>()
48-
.UseSqlServer(connectionString) // Same connection string = atomic transactions
52+
.UseSqlServer(connectionString)
4953
.Options,
5054
schemaName: "groupings");
5155
groupingContext.Database.EnsureCreated();
56+
57+
// Note: Even though each UseSqlServer() creates a new connection object,
58+
// the adapter coordinates transactions via UseTransaction() because the
59+
// connection strings match and SQL Server supports transaction coordination.
5260
```
5361

5462
**Other configuration options:**

0 commit comments

Comments
 (0)