Experiment: Change transaction management #126
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is to share an idea to change the current transaction management in TRex
Right now transactions are managed by a Transaction middleware that is configured in the API server
This PR changes transaction management to happen at the service layer
Createfunction manages the tx, but the code to create the dinosaur and send an event is in thecreatefunction (note the case)This PR is far from definitive, the transaction manager could be:
One downside of doing tx in the service layer is the reference to db concerns, not abstracted in DAOs.
It is similar in this case to advisory lock management
Assisted-by: Claude
--- from here on, generated by claude ---
Transaction Management in TRex
Why We Don't Use
db.MarkForRollbackAnymoreThis document explains the evolution of transaction management in the TRex application and why we moved away from manual rollback tracking.
Old Approach (Middleware-based)
How it worked
Problems
New Approach (Service-layer)
How it works
Key difference
Why This is Better
1. Automatic Management
GORM's
.Transaction()automatically:2. Simpler Code
DAOs are cleaner - just return errors, don't need to call
MarkForRollback:3. Natural Error Propagation
Errors flow up naturally:
4. Explicit Boundaries
Transaction boundaries are clear in the code:
5. Better Separation of Concerns
Architecture Comparison
Old Architecture
New Architecture
Example: Create Dinosaur
Old Implementation
New Implementation
Transaction Behavior
When Transaction Commits
When Transaction Rolls Back
What Gets Rolled Back
When an error occurs:
Summary
Old Way
db.MarkForRollback(ctx, err)everywhereNew Way
The
MarkForRollbackapproach was necessary with middleware-based transactions, but with service-layer transactions using GORM's built-in support, we get automatic transaction management for free!Additional Benefits
Transaction Management Refactoring
Summary
Moved database transaction management from HTTP middleware layer to service layer for better separation of concerns and more precise transaction control.
Changes Made
1. New Transaction Helper
db.WithTransaction()inpkg/db/transactions.go2. Service Layer Updates
pkg/services/dinosaurs.gosessionFactoryparameter to service constructor3. Session Factory Enhancement
pkg/db/db_session/default.goandtest.go4. Advisory Lock Compatibility
pkg/db/advisory_locks.go5. Middleware Removal
cmd/trex/server/routes.goTransactionMiddlewareregistration6. DAO Cleanup
pkg/dao/dinosaur.goandpkg/dao/event.godb.MarkForRollback()calls7. Test Infrastructure
pkg/db/mocks/session_factory.gopkg/services/dinosaurs_test.gotest/integration/dinosaurs_test.goBenefits
Migration Notes
For new services or entities:
db.WithTransaction()in service methods that modify datadb.MarkForRollback()sessionFactoryto service constructorsExample: