Luminova Framework

Database Connection

Last updated: 2024-05-13 06:14:02

This class allows you to initiate a database connection, Comprehensive Guide to Using the Database Connection Class for Efficient Data Management to get started, see Database Example.

  • Class namespace: \Luminova\Database\Connection

Properties

Database connection instance

protected \Luminova\Interface\DriversInterface|null $db

Methods

constructor

Initializes the Connection class constructor based on configuration in the .env file.

new Connection()
  • The constructor method is final.

Throws:


database

Retrieves the database connection instance.

public database(): \Luminova\Interface\DriversInterface|null

Return Value:

DriversInterface|null - The database driver connection instance if connected; otherwise, returns null.

To get the raw database connection instance of PDO or mysqli.

<?php 
$conn->database()->raw()

getInstance

Retrieves the shared singleton instance of the Connection class.

public static getInstance(): self

Return Value:

self - Connection class instance.

Throws:


newInstance

Retrieves a new database driver instance based on the provided configuration.

If no configuration is provided, the default configuration will be used.

public static newInstance(\Luminova\Base\BaseDatabase|null $config = null): \Luminova\Interface\DriversInterface|null

Parameters:

ParameterTypeDescription
$config\Luminova\Base\BaseDatabase|nullDatabase configuration (default: null).

Return Value:

DriversInterface|null - Database driver instance.

Throws:


connect

Connects to the database, either returning a connection instance or reusing a previous connection from the pool if available.

public connect(): \Luminova\Interface\DriversInterface|null

Return Value:

DriversInterface|nul - Database driver instance (either MySqliDriver or PdoDriver).

Throws:

Optionally retries failed connections based on the retry attempt value set in the .env file (database.connection.retry).


retry

Retries the database connection with optional backup server fallback.

public retry(int|null $retry = 1): \Luminova\Interface\DriversInterface|null

Parameters:

ParameterTypeDescription
$retryint|nullNumber of retry attempts (default: 1).

Return Value:

DriversInterface|null - Connection instance or null if all retry attempts fail.

Throws:

If the retry parameter is set to null, retries the connection with backup servers if available.


release

Releases a connection back to the connection pool.

public release(\Luminova\Interface\DriversInterface|null $connection): void

Parameters:

ParameterTypeDescription
$connection\Luminova\Interface\DriversInterface|nullThe connection to release.

If the connection pool is not full, adds the provided connection to the pool else closes the provided connection.


purge

Purges all stacked pool connections and optionally closes the database connection.

public purge(bool $conn = false): void

Parameters:

ParameterTypeDescription
$connboolIf true, close the database connection. Default is false.

If the conn parameter is true, the database connection will be closed; otherwise, only the pool connections will be closed.