PHP Luminova: Database Connection Configuration and Failover
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
- Full namespace:
Luminova\Base\Database\AbstractConfig - This class is abstract.
- This class implements Luminova\Interface\LazyObjectInterface.
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
| Name | Type | Description |
|---|---|---|
host | string | Server hostname. Defaults to localhost. |
port | int | Server port. Defaults to 3306. |
database | string | Database name. Defaults to an empty string. |
connection | string | Connection implementation, pdo or mysqli. Defaults to pdo. |
pdo_driver | string | PDO driver, such as mysql or sqlite. Defaults to mysql. |
username | string | Database username. Defaults to root. |
password | string | Database password. Defaults to an empty string. |
charset | string | Connection character set. Defaults to utf8mb4. |
sqlite_path | string|null | SQLite database path. Defaults to null. |
socket | bool | Enable socket-based connection. Defaults to false. |
socket_path | string | Socket path. Defaults to an empty string. |
persistent | bool | Enable persistent connections. Defaults to false. |
timeout | int | Connection timeout in seconds. Defaults to 0. |
emulate_prepares | bool | Prepared-statement emulation setting. Defaults to true. |
buffered_query | bool | Enable buffered queries for MySQL PDO. Defaults to false. |
commands | string[] | 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:
| Parameter | Type | Description |
|---|---|---|
$config | array<string, mixed> | Settings keyed by name (default []). |
getServers
Retrieve all database connection servers.
public static final function getServers(): arrayReturn:
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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The property configuration key. |
$default | mixed | Optional 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): staticParameters:
| Parameter | Type | Description |
|---|---|---|
$config | array<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(): arrayReturn:
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|nullReturn:
string|int|null - Key in $databaseServers. The base implementation returns null, which leaves the connection without a configured shard.