Skip to content

feat(database): implement transaction middleware with ACID#404

Merged
phertyameen merged 1 commit intoMindBlockLabs:mainfrom
mijinummi:feat/db-transaction-middleware-acid-322
Mar 27, 2026
Merged

feat(database): implement transaction middleware with ACID#404
phertyameen merged 1 commit intoMindBlockLabs:mainfrom
mijinummi:feat/db-transaction-middleware-acid-322

Conversation

@mijinummi
Copy link
Copy Markdown
Contributor

🚀 #322 Database Transaction Middleware for Data Integrity

📌 Overview

This PR introduces a robust transaction middleware layer to enforce ACID guarantees across all database operations. It ensures that every request is executed within a controlled transactional context, preventing partial writes, data inconsistencies, and connection leaks.


🎯 Key Features

✅ Automatic Transaction Management

  • All database operations are wrapped in a transaction by default
  • Transactions are committed on success and rolled back on failure

🔐 Isolation Levels Support

  • READ COMMITTED (default)
  • REPEATABLE READ (critical operations)
  • SERIALIZABLE (leaderboards, competitions)

🔁 Nested Transactions (Savepoints)

  • Supports nested transactional flows using savepoints
  • Prevents partial failures in complex service orchestration

⏱ Timeout & Deadlock Handling

  • Configurable transaction timeout
  • Automatic rollback on timeout
  • Deadlock detection and graceful retry/failure handling

📖 Read-Only Transactions

  • Optimized transactions for read-heavy endpoints
  • Reduces locking and improves performance

🧠 Manual Transaction Control

  • Allows overriding default behavior for advanced use cases

📊 Logging & Observability

  • Logs:
    • transaction start/end
    • rollbacks
    • deadlocks
    • timeouts
  • Enables future integration with monitoring systems

🔌 TypeORM Integration

  • Uses QueryRunner for full lifecycle control
  • Ensures proper connection release (no leaks)

🧪 Covered Scenarios

Scenario Behavior
Puzzle submission Atomic update of progress, XP, streak
User registration User + streak + email handled transactionally
Points claiming XP update + transaction log + achievements
Friend request Relationship + notification consistency

⚠️ Rollback Triggers

  • Validation errors
  • Database constraint violations
  • Business logic failures
  • External API failures (critical paths)
  • Transaction timeout exceeded

🧩 Edge Case Handling

  • Deadlock resolution strategy implemented
  • Nested transaction conflict safety
  • Connection pool exhaustion protection
  • Safe cleanup on all error paths

✅ Acceptance Criteria Checklist

  • All DB operations are transactional by default
  • Failed requests rollback completely
  • Successful requests commit atomically
  • Deadlocks handled gracefully
  • Transaction timeouts enforced
  • Read-only optimization supported
  • Multi-entity operations remain consistent
  • No connection leaks observed
  • Logging and metrics implemented

Closes #322

@phertyameen phertyameen merged commit 748a81d into MindBlockLabs:main Mar 27, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database Transaction Middleware for Data Integrity

2 participants