Database Quick Start (Based on Laravel Database Component)

webman/database is developed based on illuminate/database and includes a connection pool feature, supporting both coroutine and non-coroutine environments. Its usage is the same as Laravel.

Developers can also refer to the Using Other Database Components section to use ThinkPHP or other databases.

Note
This manual is for webman-v2. If you are using webman-v1, please refer to the v1 version manual.

Database Installation

composer require -W webman/database illuminate/pagination illuminate/events symfony/var-dumper

After installation, a restart is required (reload will not work).

Tip
webman/database depends on Laravel's illuminate/database, so its dependency packages will be automatically installed during the installation.

Note
If pagination, database events, and SQL logging are not needed, you only need to execute
composer require -W webman/database.

Database Configuration

config/database.php


return [
    // Default database
    'default' => 'mysql',

    // Various database configurations
    'connections' => [
        'mysql' => [
            'driver'      => 'mysql',
            'host'        => '127.0.0.1',
            'port'        => 3306,
            'database'    => 'test',
            'username'    => 'root',
            'password'    => '',
            'unix_socket' => '',
            'charset'     => 'utf8',
            'collation'   => 'utf8_unicode_ci',
            'prefix'      => '',
            'strict'      => true,
            'engine'      => null,
            'options' => [
                PDO::ATTR_EMULATE_PREPARES => false, // Required when using swoole or swow as the driver
            ],
            'pool' => [ // Connection pool configuration
                'max_connections' => 5, // Maximum number of connections
                'min_connections' => 1, // Minimum number of connections
                'wait_timeout' => 3,    // Maximum wait time for obtaining a connection from the pool; an exception will be thrown after timeout. Effective only in coroutine environment
                'idle_timeout' => 60,   // Maximum idle time for connections in the pool; connections will be closed and recycled after timeout until the number is min_connections
                'heartbeat_interval' => 50, // Heartbeat detection time for the connection pool, in seconds, recommended to be less than 60 seconds
            ],
        ],
    ],
];

Except for the pool configuration, other configurations are the same as Laravel.

About Connection Pool

  • Each process has its own connection pool, and connection pools are not shared between processes.
  • When coroutines are not enabled, business tasks are executed in the process queue, and concurrency will not occur, so the connection pool will have at most 1 connection.
  • When coroutines are enabled, business tasks are executed concurrently within the process, and the connection pool will dynamically adjust the number of connections as needed, with a maximum of max_connections and a minimum of min_connections.
  • Because the maximum number of connections in the connection pool is max_connections, when the number of coroutines operating on the database exceeds max_connections, some coroutines will queue up waiting, and will wait for a maximum of wait_timeout seconds; if exceeded, an exception is triggered.
  • In idle situations (including both coroutine and non-coroutine environments), connections will be reclaimed after idle_timeout time, until the number of connections reaches min_connections (min_connections can be 0).

Database Usage Example

<?php
namespace app\controller;

use support\Request;
use support\Db;

class UserController
{
    public function db(Request $request)
    {
        $default_uid = 29;
        $uid = $request->get('uid', $default_uid);
        $name = Db::table('users')->where('uid', $uid)->value('username');
        return response("hello $name");
    }
}

As we can see, the usage is the same as in Laravel, using the Db::table() method to operate on the database.