Video summary

Golang Microservices Full Course #06 – Database Transactions (BEGIN, COMMIT & ROLLBACK)

Main summary

Key takeaways

Technology

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 succeed
  • tx.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 model
    • wallet repository
    • wallet service
    • wallet 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:

  1. Insert the user into the users table using the transaction connection
  2. Create the wallet for the new user using the same transaction
  3. Commit only if both inserts succeed
  4. Rollback on any failure

Repository Adjustment

The user repository is updated to support transaction-based creation, e.g.:

  • Adding methods like CreateTX
  • Passing a *sql.Tx into 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 userID from 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:
    • login
    • register

Implementation / Environment Notes

  • Middleware setup discussion:
    • gin.default vs gin.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-transaction after verification

Main Speakers / Sources

  • Primary speaker: the course instructor (coding and presenting throughout)
  • Codebase sources: “Golang Microservices Full Course” repository
    • Monolith internal folders: user and wallet
    • Includes Gin routing and SQL repositories/services

Rate this summary

Your feedback will help improve summaries.

Improve this summary

Reprocess with a stronger model when the summary feels incomplete or inaccurate.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video