Video summary
Golang Microservices Full Course #06 – Database Transactions (BEGIN, COMMIT & ROLLBACK)
Main summary
Key takeaways
Course Focus (Episode #06): Go Golang Microservices — Database Transactions
- The episode builds from earlier auth-related work (JWT + bcrypt password hashing/comparison) toward wallet management and balance checking.
- It highlights why database transactions are required when writing to multiple tables (e.g., creating a user and a wallet).
Why Transactions Are Needed
When multiple operations must succeed together:
- If one step fails (e.g., wallet insert fails), you must not keep partial changes (e.g., the user row).
- Instead, you roll back everything so the database remains consistent.
Standard Transaction Flow
tx.Begin()- Execute multiple operations using the transaction connection
tx.Commit()only if all operations succeedtx.Rollback()if any error occurs (or a panic happens)
Wallet Feature Being Implemented
The episode adds a wallet module under a monolith structure like:
monolith/internal/wallet/wallet modelwallet repositorywallet servicewallet handler
Wallet Model Fields (Conceptual)
ID(string, generated as UUID)UserID(string)Balance(numeric type)Currency(e.g.,IDR)Status(string; e.g., active/frozen)Version(an integer mentioned, but ultimately not used/stored in inserts)
Wallet Repository: SQL + Transaction Support
Repository Responsibilities
A walletRepository interface supports:
- Transaction-aware wallet creation (conceptually like
CreateTX(ctx, tx, wallet)) - Fetching wallet by user ID:
GetByUserID(ctx, userID)
Example SQL Operations
- Insert wallets:
INSERT INTO wallets (id, user_id, balance, currency, status) ...
- Select wallet:
SELECT ... FROM wallets WHERE user_id = ?
Error Handling
- If the lookup query returns
sql.ErrNoRows, the repository returns a custom “wallet not found” application error.
Transaction Refactor Applied to User Registration
The user registration flow in user service is refactored to wrap multiple DB operations into one transaction:
- Insert the user into the
userstable using the transaction connection - Create the wallet for the new user using the same transaction
Commitonly if both inserts succeedRollbackon any failure
Repository Adjustment
The user repository is updated to support transaction-based creation, e.g.:
- Adding methods like
CreateTX - Passing a
*sql.Txinto creation functions
Key Transaction Logic (As Described)
- Begin:
tx, err := db.BeginTx(ctx, nil)
- If any error occurs:
tx.Rollback()
- If both inserts succeed:
tx.Commit()
Wallet Service + Handler + Protected Route
Wallet Service Method
Adds a method:
GetWalletByUserID(ctx, userID)- Returns the wallet record, or a “not found” app error
Gin Handler
A Gin handler:
- Extracts
userIDfrom JWT-related context (c.get(...)from Gin/JWT middleware) - Calls the wallet service
- Returns JSON containing wallet data
Routes
- Protected endpoint (under JWT-protected routes):
GET /wallets.me
- Public routes remain grouped for:
loginregister
Implementation / Environment Notes
- Middleware setup discussion:
gin.defaultvsgin.new- Adding
gin.Recovery()to recover from panics and prevent crashes
- Instructor testing confirms:
- Registering a user triggers automatic wallet creation
- Returned wallet fields (e.g.,
balance, wallet/user IDs) match expected values
- Code merge note:
- Changes were merged into a branch such as
feature/006-database-transactionafter verification
- Changes were merged into a branch such as
Main Speakers / Sources
- Primary speaker: the course instructor (coding and presenting throughout)
- Codebase sources: “Golang Microservices Full Course” repository
- Monolith internal folders:
userandwallet - Includes Gin routing and SQL repositories/services
- Monolith internal folders:
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.