Skip to content

Conversation

@ahopkins
Copy link
Owner

This pull request introduces several major improvements to the Mayim library, focusing on enhanced connection pool management, improved transaction coordination, and expanded configuration options. The most significant changes include the introduction of a shared pool registry to prevent duplicate connections, support for connection pool sizing, a more flexible transaction API (including two-phase commit and timeouts), and compatibility updates for newer Python versions.

Connection Pool Management and Sizing:

  • Introduced PoolRegistry to ensure that executors using the same DSN share a single pool instance, preventing duplicate connections and enabling proper transaction coordination (src/mayim/registry.py, src/mayim/mayim.py, tests/conftest.py). [1] [2] [3] [4]
  • Added support for configuring minimum and maximum pool sizes via min_size and max_size parameters in BaseInterface and its subclasses, and ensured these are passed through the registry and pool constructors (src/mayim/base/interface.py, src/mayim/lazy/interface.py, src/mayim/sql/postgres/interface.py, src/mayim/mayim.py). [1] [2] [3] [4] [5] [6] [7]

Transaction Coordination and API Improvements:

  • Refactored the Mayim.transaction API to support both old and new usage styles, added options for two-phase commit (use_2pc) and transaction timeouts, and introduced a wrapper for backward compatibility. The new API ensures only registered SQL executors are used and supports both context manager and awaitable usage (src/mayim/mayim.py).
  • Updated SQLExecutor to honor global transaction context, ensuring that rollbacks and transaction context managers interact correctly with coordinated transactions (src/mayim/sql/executor.py). [1] [2]

Python Compatibility and Miscellaneous:

  • Declared support for Python 3.11, 3.12, and 3.13 in setup.cfg and bumped the package version to 1.2.0. [1] [2]
  • Minor code cleanups: replaced ellipsis methods with single-line stubs for clarity and consistency (src/mayim/exception.py, src/mayim/extension/quart_extension.py, src/mayim/extension/starlette_extension.py, src/mayim/base/executor.py, src/mayim/base/interface.py, src/mayim/lazy/interface.py). [1] [2] [3] [4] [5]

These changes collectively modernize the codebase, improve resource management, and provide more robust transactional guarantees for multi-database operations.

ahopkins and others added 10 commits September 14, 2025 20:00
This commit adds comprehensive transaction management improvements to Mayim:

Core Features:
- New transaction coordinator system for managing cross-executor transactions
- Two-phase commit protocol support for distributed transactions
- Proper connection sharing across executors in global transactions
- Transaction state machine (PENDING, ACTIVE, COMMITTED, ROLLED_BACK)

API Enhancements:
- New explicit transaction API: txn = await Mayim.transaction(); await txn.begin()
- Backward compatible with existing context manager pattern
- Support for mixing executor classes and instances in transactions
- Optional use_2pc parameter for enabling two-phase commit

Bug Fixes:
- Fixed DSN parsing to handle missing ports with sensible defaults
- Fixed connection sharing across executors with same DSN
- Fixed transaction context propagation through ContextVars
- Fixed nested transaction handling (now properly no-ops when in global transaction)

Testing:
- Added comprehensive test suite for transaction failures
- Added tests for new explicit transaction API
- Added advanced transaction tests (deadlock detection, ACID compliance, etc.)
- All tests passing except unimplemented optional features (marked as xfail)

Technical Details:
- GlobalTransactionContext manages connections across multiple executors
- PoolRegistry ensures executors with same DSN share pool instances
- Transaction coordinator handles begin/commit/rollback lifecycle
- Proper cleanup and error handling throughout

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Exclude Claude Code session files from version control.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ahopkins ahopkins merged commit 56858d8 into main Sep 15, 2025
6 checks passed
@ahopkins ahopkins deleted the better-transaction-support branch September 15, 2025 15:07
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.

2 participants