go.gorm.session_management
Stability
High
Detects improper GORM session management patterns.
Why It Matters
Section titled “Why It Matters”Improper session management:
- Connection leaks — Sessions not closed properly
- Stale data — Reusing cached session state
- Race conditions — Sharing sessions across goroutines
Example
Section titled “Example”// ❌ Before (reusing global db instance unsafely)var db *gorm.DB
func GetUser(id uint) User { var user User db.First(&user, id) // May have stale session state return user}// ✅ After (using fresh session)var db *gorm.DB
func GetUser(id uint) User { var user User db.Session(&gorm.Session{}).First(&user, id) return user}
// Or with contextfunc GetUserWithContext(ctx context.Context, id uint) User { var user User db.WithContext(ctx).First(&user, id) return user}What Unfault Detects
Section titled “What Unfault Detects”- Sharing DB instances across goroutines without session isolation
- Missing WithContext in request handlers
- Stale session reuse patterns