使用eloquent orm和mysql的连接太多了

Hem*_*wat 7 php mysql orm slim eloquent

我正在使用SLIM FrameworkLaravel Eloquent ORM用于REST API.最近我遇到了一个问题too many connections.

在一个请求URI期间,我需要进行多次调用GetSet调用mySql DB.这将打开我所做的每个数据库事务的连接.我想避免这种情况.现在,mysql连接池有200个线程.

我的API预计将有超过1000个并发调用,并且在当前环境中,40%的调用将失败(使用jMeter测试).

我的想法是,对于一个API调用,我的应用程序应该只使用一个连接线程,并将MySql连接池增加到大约1000到1500之间.这是一个糟糕的方法吗?

使用Eloquent ORM,我的数据库连接由Capsule管理. 我应该使用Singleton方法和API请求中的任何后续调用进行第一次连接,应该使用相同的线程吗?

这是我的数据库连接管理器:

    use Illuminate\Database\Capsule\Manager as Capsule;
    /**
     * Configure the database and boot Eloquent
     */
    $capsule = new Capsule;

    $capsule->addConnection($databaseConfig['mysql']);

    // Set the event dispatcher used by Eloquent models... (optional)
    use Illuminate\Events\Dispatcher;
    use Illuminate\Container\Container;

    $dispatcher = new Dispatcher(new Container);
    $capsule->setEventDispatcher($dispatcher);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();
Run Code Online (Sandbox Code Playgroud)

解决这个问题的最佳方法是什么?

UPDATE

我正在尝试另一种方法来建立持久连接.但是,在完成对作业的调用之后,持久连接仍未关闭.即使打电话DB::Disconnect也无济于事.

    <?php

    use Illuminate\Database\Capsule\Manager as Capsule;
    use Illuminate\Events\Dispatcher;
    use Illuminate\Container\Container;

    /**
     * Configure the database and boot Eloquent
     */
    $app->hook('slim.before', function() use ($app) {
        try {

    //        pr('', $app->settings['databaseConfig']['mysql'], 1);
            /*
             * Register Eloquent as singleton to slim container
             * since we will use the same instance across the request cycle
             */
            $app->container->singleton('db', function() {
                return new Capsule;
            });

            $app->db->addConnection($app->settings['databaseConfig']['mysql']);

            $dispatcher = new Dispatcher(new Container);
            $app->db->setEventDispatcher($dispatcher);

            if (isset($app->settings['databaseConfig']['profiler']) && $app->settings['databaseConfig']['profiler']) {
                $dispatcher->listen('illuminate.query', function($sql, $params, $time, $conn) {

                    dd(array($sql, $params, $time, $conn));
                });
            }

            $app->db->setAsGlobal();
            $app->db->bootEloquent();
        } catch (PDOException $e) {
            /** Do some stuff to handle exception */
            echoResponse(501, array('No DB Connections'));
        }
    });
Run Code Online (Sandbox Code Playgroud)

Rob*_*len 1

您应该对所有查询使用相同的数据库连接。最简单的方法是连接到 DI 容器中的数据库,因为您可以在每次需要时再次将其拉出。

使用 Slim 3,它看起来像这样:

$container = $app->getContainer();

$container['db'] = function ($container) {
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($container['settings']['db']);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};
Run Code Online (Sandbox Code Playgroud)

您现在可以在可调用的路由中使用:

$app->get('/list', function ($request, $response) {
    $table = $this->get('db')->table('table_name');
    $data = $table->get();

    // do something with data
    $response->write(print_r($data, true));

    return $response;

});
Run Code Online (Sandbox Code Playgroud)

此处文档中的完整详细信息:http ://www.slimframework.com/docs/cookbook/database-eloquent.html