Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Transaction Manager Mysql Laravel Package

aeatech/transaction-manager-mysql

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require aeatech/transaction-manager-mysql
    

    Add the service provider to config/app.php:

    'providers' => [
        // ...
        Aeatech\TransactionManager\TransactionManagerServiceProvider::class,
    ],
    
  2. Basic Usage Inject the TransactionManager facade into your controller/service:

    use Aeatech\TransactionManager\Facades\TransactionManager;
    
    public function executeTransaction()
    {
        TransactionManager::begin();
        try {
            // Execute queries
            DB::table('users')->insert(['name' => 'John']);
            DB::table('posts')->insert(['user_id' => 1, 'title' => 'Hello']);
    
            TransactionManager::commit();
        } catch (\Exception $e) {
            TransactionManager::rollback();
            throw $e;
        }
    }
    
  3. First Use Case Replace manual DB::beginTransaction()/commit()/rollback() calls with this package for:

    • Atomic operations (e.g., order processing with inventory updates).
    • Readability (centralized transaction handling).

Implementation Patterns

Workflow Integration

  1. Service Layer Transactions Encapsulate transactions in services:

    class OrderService {
        public function createOrder(Order $order) {
            TransactionManager::begin();
            try {
                $order->save();
                $order->inventory->decrement();
                TransactionManager::commit();
            } catch (\Exception $e) {
                TransactionManager::rollback();
                throw new OrderFailedException($e);
            }
        }
    }
    
  2. Middleware for Global Transactions Use middleware to auto-wrap requests:

    class TransactionMiddleware {
        public function handle($request, Closure $next) {
            TransactionManager::begin();
            try {
                $response = $next($request);
                TransactionManager::commit();
                return $response;
            } catch (\Exception $e) {
                TransactionManager::rollback();
                throw $e;
            }
        }
    }
    
  3. Query Builder Integration Chain with Laravel’s query builder:

    TransactionManager::begin();
    DB::table('users')
        ->where('active', 1)
        ->update(['last_login' => now()]);
    TransactionManager::commit();
    

Advanced Patterns

  • Nested Transactions (if supported):

    TransactionManager::begin();
    // Outer transaction
    TransactionManager::savepoint('user_update');
    try {
        DB::table('users')->update(['status' => 'active']);
        TransactionManager::release('user_update'); // Commit savepoint
    } catch (\Exception $e) {
        TransactionManager::rollback('user_update'); // Rollback to savepoint
    }
    
  • Transaction Retry Logic:

    $attempts = 0;
    while ($attempts < 3) {
        try {
            TransactionManager::begin();
            // Critical operation
            TransactionManager::commit();
            break;
        } catch (\Exception $e) {
            TransactionManager::rollback();
            $attempts++;
            sleep(1);
        }
    }
    

Gotchas and Tips

Pitfalls

  1. No Explicit Savepoints Unlike DB::transaction(), this package may not support savepoints. Avoid relying on them unless documented.

  2. Connection Binding Ensure the package uses the same DB connection as your queries:

    TransactionManager::connection('mysql_replica')->begin();
    
  3. Exception Handling Uncaught exceptions will trigger rollbacks. Use try-catch blocks or middleware to handle failures gracefully.

  4. Long-Running Transactions MySQL has a transaction_isolation limit (e.g., REPEATABLE READ). Avoid holding locks for >1 hour.

Debugging Tips

  • Log Transactions Wrap calls in Log::debug() to track transaction boundaries:

    Log::debug('Transaction started', ['scope' => 'order_creation']);
    
  • Check for Deadlocks MySQL errors like 1213 (Deadlock found) indicate transaction conflicts. Add retry logic or optimize query order.

Extension Points

  1. Custom Rollback Logic Override the default rollback behavior by binding to the transaction.rollback event:

    Event::listen('transaction.rollback', function ($transaction) {
        // Custom cleanup (e.g., log failed transaction)
    });
    
  2. Transaction Metadata Attach metadata to transactions for auditing:

    TransactionManager::begin(['user_id' => auth()->id(), 'action' => 'update_profile']);
    
  3. Testing Mock transactions in tests:

    $this->partialMock(TransactionManager::class, ['begin', 'commit', 'rollback'])
         ->shouldReceive('begin')
         ->once();
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ilhamsyabani/laravel-volt-starter
thethunderturner/filament-latex
ghostcompiler/laravel-querybuilder
webrek/laravel-telescope-mongodb
anousss007/blatui
zatona-eg/zatona-eg-api
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat