[13.x] Savepoint Support for Database Transactions #55996
+2,064
−52
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
💡 Description
This pull request introduces an API for utilizing database transaction savepoints (while fully respecting the framework’s usage of savepoints under the hood for simulating nested transactions) giving developers greater control over database commit logic and enabling the use of more complex failure handling patterns.
+ Features:
+ Benefits:
⚖️ Justification
⁇ Problem
Laravel's transaction system lacks intermediate rollback points, forcing developers to choose between rolling back entire transactions or implementing complex workarounds for partial rollbacks in multi-step operations.
Real-World Limitations:
Industry Standards Gap:
‼ Solution
Native savepoint management with automatic lifecycle handling, context-aware error messages, and seamless Laravel integration.
⚙️ Usage Examples
Real-World Scenarios
Advanced Error Handling
Database Compatibility
📔 API Refence
🏗️ Technical Implementation
Core Architecture
Automatic Transaction Integration:
Savepoint Name Sanitization:
Context-Aware Error Messages:
Savepoint Lifecycle Management
Callback-Based Execution:
Transaction Level Isolation:
Database Grammar Support
SAVEPOINT
RELEASE
SAVEPOINT
RELEASE
SAVE TRANSACTION
SAVEPOINT
🪵 Changelog
⿲ Sequence Diagram
♻️ Backward Compatibility
This feature targets
13.x
asConnectionInterface
now has additional methods.✅ Testing
44 tests (199 assertions) cover complete functionality:
Core Functionality
✓ Savepoint Creation - Basic savepoint creation and naming
✓ Rollback Operations - Partial rollback to specific savepoints
✓ Release Management - Proper savepoint cleanup and release
✓ Callback Execution - Automatic success/failure handling
Error Handling & Security
✓ Context-Aware Errors - Detailed debugging with transaction context
✓ SQL injection protection - Hex encoding validation for all names
✓ Database capability detection - Proper support flag handling
✓ Transaction lifecycle - Outside-transaction error conditions
Advanced Scenarios
✓ Nested Transactions - Multi-level savepoint isolation
✓ Event Integration - Created/Released/RolledBack event firing
✓ Stack Management - Complex savepoint hierarchy handling
✓ Database Grammar Compatibility - MySQL/PostgreSQL/SQLite/SQLServer
Production Edge Cases
✓ PDO Failure Handling - Database operation error recovery
✓ Memory Management - Savepoint cleanup on transaction end
✓ Concurrent Operations - Thread-safe savepoint management
✓ Large Transaction Trees - Deep nesting and cleanup validation
Framework Integration
✓ Events - Proper event dispatcher integration
✓ Transaction Helpers - Works with existing
transaction()
method✓ Connection Lifecycle - Automatic initialization and cleanup
✓ Multi-Connection Support - Independent savepoint management per connection
💁🏼♂️ The author is available for hire -- inquire at yitzwillroth@gmail.com.