Skip to content

[Mocha] Fix PostgreSQL outbox persistence connection lifetime - #10478

Merged
PascalSenn merged 3 commits into
mainfrom
fix/postgres-outbox-connection-lifetime
Oct 4, 2026
Merged

PascalSenn merged 3 commits into
mainfrom
fix/postgres-outbox-connection-lifetime

Conversation

@tobias-tengler

@tobias-tengler tobias-tengler commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

When the outbox opens a database connection to store a message, it leaves that connection open. An application may use a scoped context for the outbox and separate contexts from IDbContextFactory for repository operations, all within one TransactionScope. A later repository operation then opens another connection while the outbox connection is still open and can fail with “prepared transactions are disabled”.

The outbox now closes connections it opens after persistence, including on failure or cancellation. Connections already open remain open, and application writes and outbox messages still commit or roll back together.

Balance EF connection opening and closing in guaranteed cleanup so persistence preserves caller-owned connections and transaction enlistment.

Add direct PostgreSQL regression coverage for ambient and explicit transaction atomicity, pooled factory contexts, repeated persistence, failures, and cancellation with prepared transactions disabled.
Copilot AI balanced review requested due to automatic review settings October 4, 2026 09:26
@tobias-tengler tobias-tengler changed the title Fix PostgreSQL outbox persistence connection lifetime [Mocha] Fix PostgreSQL outbox persistence connection lifetime Oct 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused lifecycle fix is comprehensively covered across success, failure, cancellation, and transaction scenarios.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes PostgreSQL outbox connection leaks while preserving caller-managed connections and transaction enlistment.

Changes:

  • Balances EF-managed connection opening and closing with guaranteed cleanup.
  • Adds PostgreSQL regression coverage for transactions, pooling, failures, and cancellation.
File Description
PostgresOutboxConnectionLifetimeTests.cs Adds connection-lifetime regression tests.
Mocha.EntityFrameworkCore.Postgres.Tests.csproj Adds snapshot-testing dependencies.
PostgresMessageOutbox.cs Restores connection state after persistence.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

100.0% of changed lines covered (17/17)

File Covered Changed Patch %
…/Outbox/PostgresMessageOutbox.cs 17 17 100.0% 🟢

Project coverage: 57.9% (300812/519757 lines)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants