fix: [#840] DB cache is not reset when fresh orm#1312
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v1.16.x #1312 +/- ##
==========================================
Coverage ? 67.99%
==========================================
Files ? 216
Lines ? 11609
Branches ? 0
==========================================
Hits ? 7894
Misses ? 3335
Partials ? 380 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| // TODO: The fresh logic needs to be optimized, it's a bit unclear now. | ||
| func (r *Orm) Fresh() { | ||
| r.fresh(binding.Orm) | ||
| driver.ResetConnections() |
There was a problem hiding this comment.
It only works with facades.Orm().Fresh(), but calling facades.App().Fresh() only still results in a cache hit
https://github.com/goravel/framework/blob/master/foundation/container.go#L73
There was a problem hiding this comment.
facades.Orm().Fresh() is called in the Ready function, so it works for your case in goravel/example. And as the annotation said, the fresh flow should be optimized.
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.
| Benchmark suite | Current: e6961b2 | Previous: 06202a1 | Ratio |
|---|---|---|---|
Benchmark_Debug |
125562 ns/op 34328 B/op 513 allocs/op |
81915 ns/op 23407 B/op 384 allocs/op |
1.53 |
Benchmark_Debug - ns/op |
125562 ns/op |
81915 ns/op |
1.53 |
Benchmark_Info |
124719 ns/op 34163 B/op 511 allocs/op |
82307 ns/op 23358 B/op 384 allocs/op |
1.52 |
Benchmark_Info - ns/op |
124719 ns/op |
82307 ns/op |
1.52 |
Benchmark_Warning |
131584 ns/op 35186 B/op 527 allocs/op |
83765 ns/op 23403 B/op 384 allocs/op |
1.57 |
Benchmark_Warning - ns/op |
131584 ns/op |
83765 ns/op |
1.57 |
Benchmark_Warning - B/op |
35186 B/op |
23403 B/op |
1.50 |
Benchmark_Panic |
174330 ns/op 52525 B/op 708 allocs/op |
114612 ns/op 36604 B/op 546 allocs/op |
1.52 |
Benchmark_Panic - ns/op |
174330 ns/op |
114612 ns/op |
1.52 |
This comment was automatically generated by workflow using github-action-benchmark.
There was a problem hiding this comment.
Pull request overview
This pull request addresses issue #840 by implementing a mechanism to reset database connections when the ORM is refreshed. The changes convert the connection cache from a regular map to a sync.Map for better concurrency handling and introduce a new ResetConnections() function that is called during ORM refresh operations.
Key Changes:
- Converted
connectionToDBfrommap[string]*gorm.DBtosync.Mapfor thread-safe operations - Added
ResetConnections()function to clear the database connection cache - Integrated connection reset into the
Orm.Fresh()method
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| database/driver/gorm.go | Converted connection cache to sync.Map and added ResetConnections function to clear cached connections |
| database/orm/orm.go | Added driver import and called ResetConnections in Fresh method to ensure connections are reset during ORM refresh |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📑 Description
Closes goravel/goravel#840
This pull request introduces an improvement to the database connection handling by adding a mechanism to reset GORM connections, and integrates this reset into the ORM's
Freshmethod. The changes also include a minor import update.Database connection management improvements:
ResetConnectionsfunction todatabase/driver/gorm.gothat safely clears the internal map of GORM connections, allowing for a clean reset of database connections.Freshmethod inOrm(database/orm/orm.go) to calldriver.ResetConnections(), ensuring that all GORM connections are reset when refreshing the ORM state.Codebase maintenance:
github.com/goravel/framework/database/driverindatabase/orm/orm.goto support the new connection reset functionality.✅ Checks