Luminova Framework

PHP Luminova: Database Connection Configuration and Failover

Last updated: 2026-11-28 20:57:12

Configure database servers for connection failover and route connections to a selected shard.

The Database Configuration Abstract Class is a base for defining a database connection configuration. Your subclass supplies connection settings and can declare backup servers or select a shard. Without a selected shard, Luminova retries the primary connection before trying the backup servers in order.

When sharding is enabled and a shard identifier is available, Luminova looks up that identifier in $databaseServers. A missing or empty shard entry raises a database exception. For a configured shard that fails to connect, enable $shardFallbackOnError to allow attempts against other configured servers.

See Database Connection for connection setup and ORM Query Builder for querying.


Example

Here is an example on how you can extend and use the base server.

// /app/Config/Database.php

namespace App\Config;

use Luminova\Base\Database\AbstractConfig;

class Database extends AbstractConfig
{
    public static bool $connectionSharding = true;

    /**
     * @var array<int|string, array<string, mixed>>
     */
    protected static array $databaseServers = [
        'NG' => [
            'host'          => 'ng.db.server',
            'port'          => 3306,
            'database'      => 'my_db_ng',
            'connection'    => 'pdo',
            'pdo_driver'   => 'mysql',
            'username'      => 'user_ng',
            'password'      => 'secret',
            'charset'       => 'utf8mb4',
            'persistent'    => true,
            'emulate_prepares' => true,
            'socket'        => false,
            'timeout'       => 0,
            'socket_path'   => '',
            'sqlite_path'   => '', /* For the SQLite PDO driver */
        ],
        // Add settings for the US and UK servers here.
        /* More connection servers ... */
    ];

    public static function getShardServerIdentifier(): string|int|null
    {
        return 'NG';
    }
}

Class Definition


Properties

shardFallbackOnError

Whether to fallback to an available backup server if the selected shard is unreachable.

When enabled, Luminova can use another entry from $databaseServers if the configured shard fails to connect. The selected shard key must exist.

public static bool $shardFallbackOnError = false;

This is useful in distributed environments where high availability is required.


connectionSharding

Enable or disable connection-level database sharding.

When set to true, the application will attempt to route database connections based on shard configurations defined by the shard location identifier getShardServerIdentifier().

This allows for distributing database load across multiple shard servers for better scalability and isolation (e.g., multi-tenant or geo-based setups).

public static bool $connectionSharding = false;

With this setting disabled, Luminova uses the primary configuration unless the connection is explicitly assigned a shard.


databaseServers

List of database connection servers.

This serves two purposes:

  • Provides alternative connections for sharded environments when a shard is unavailable.
  • Acts as a failover mechanism for non-sharded environments when the main database fails.

Each server should follow the same structure as the primary configuration.

/**
 * @var array<int|string, array<string, mixed>>
 */
protected static array $databaseServers = [];

Each server entry can override the following connection settings. The defaults shown apply when constructing a configuration without an override. The application can supply different defaults through its primary connection configuration.

Properties

NameTypeDescription
hoststringServer hostname. Defaults to localhost.
portintServer port. Defaults to 3306.
databasestringDatabase name. Defaults to an empty string.
connectionstringConnection implementation, pdo or mysqli. Defaults to pdo.
pdo_driverstringPDO driver, such as mysql or sqlite. Defaults to mysql.
usernamestringDatabase username. Defaults to root.
passwordstringDatabase password. Defaults to an empty string.
charsetstringConnection character set. Defaults to utf8mb4.
sqlite_pathstring&#124;nullSQLite database path. Defaults to null.
socketboolEnable socket-based connection. Defaults to false.
socket_pathstringSocket path. Defaults to an empty string.
persistentboolEnable persistent connections. Defaults to false.
timeoutintConnection timeout in seconds. Defaults to 0.
emulate_preparesboolPrepared-statement emulation setting. Defaults to true.
buffered_queryboolEnable buffered queries for MySQL PDO. Defaults to false.
commandsstring[]SQL statements to run after connecting. Defaults to an empty array.

Methods


__construct

Initialize a configuration instance by overlaying the supplied settings on the built-in defaults.

public function __construct(array $config = [])

Parameters:

ParameterTypeDescription
$configarray<string, mixed>Settings keyed by name (default []).

getServers

Retrieve all database connection servers.

public static final function getServers(): array

Return:

array<int|string, array<string, mixed>> - Configured servers keyed by index or shard identifier.


getValue

Get database configuration property value.

public final function getValue(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe property configuration key.
$defaultmixedOptional default value (default: null).

Return:

mixed - Configuration value, or $default when the key is absent or its value is null.


fromArray

Create an instance of the concrete configuration class with the supplied settings.

public static function fromArray(array $config): static

Parameters:

ParameterTypeDescription
$configarray<string, mixed>Connection settings keyed by name.

Return:

static - New configuration instance.


toArray

Export the instance settings, including defaults for values the caller omitted.

public final function toArray(): array

Return:

array<string, mixed> - Connection settings. See the database configuration for individual options.


getShardServerIdentifier

Override this method in the application’s database configuration to choose the shard at runtime.

public static function getShardServerIdentifier(): string|int|null

Return:

string|int|null - Key in $databaseServers. The base implementation returns null, which leaves the connection without a configured shard.