Raxol.Database.ConnectionManager (Raxol v0.4.0)

View Source

Manages database connections and provides retry logic for Postgres errors.

This module:

  • Handles graceful connection retries
  • Monitors database connection health
  • Provides tools for debugging connection issues

Summary

Functions

Ensures the database connection is reset after application crash.

Checks if the database connection is healthy.

Forcibly restarts the database connection.

Attempts a database operation with retry logic.

Functions

ensure_connection()

@spec ensure_connection() :: :ok

Ensures the database connection is reset after application crash.

This function should be called during application startup.

healthy?()

@spec healthy?() :: boolean()

Checks if the database connection is healthy.

Returns

  • true - Connection is healthy
  • false - Connection is unhealthy

restart_connection()

@spec restart_connection() :: :ok

Forcibly restarts the database connection.

with_retries(operation, max_retries \\ 5, retry_delay_ms \\ 1000)

@spec with_retries(function(), integer(), integer()) :: {:ok, any()} | {:error, any()}

Attempts a database operation with retry logic.

Parameters

  • operation - A function that performs a database operation
  • max_retries - Maximum number of retry attempts (default: 5)
  • retry_delay_ms - Delay between retries in milliseconds (default: 1000)

Returns

  • {:ok, result} - Operation succeeded
  • {:error, error} - Operation failed after retries