简体   繁体   English

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

[英]too many connections using eloquent orm and mysql

I'm using SLIM Framework with Laravel's Eloquent ORM for REST APIs. 我正在使用SLIM Framework和Laravel的Eloquent Eloquent ORM for REST API。 Recently I faced a problem of too many connections . 最近我遇到了too many connections的问题。

During one request URI, I need to make several Get and Set calls to mySql DB. 在一个请求URI期间,我需要对mySql DB进行几次GetSet调用。 This opens connections on every DB transaction I make. 这将打开我所做的每个数据库事务的连接。 I want to avoid that. 我想避免这种情况。 Right now, mysql connection pool has 200 threads. 现在,mysql连接池有200个线程。

my API is expected to have more than 1000 concurrent calls and with the current environment, 40% of the calls will fail(tested using jMeter). 我的API预计将有超过1000个并发调用,并且在当前环境中,40%的调用将失败(使用jMeter测试)。

My idea is that for one API call, my application should use only one connection thread and increase the MySql connections pool to somewhere around 1000 to 1500 . 我的想法是,对于一个API调用,我的应用程序应该只使用一个连接线程,并将MySql连接池增加到大约1000到1500之间。 is this a bad approach? 这是一个糟糕的方法吗?

With Eloquent ORM, i have my DB connection being managed by Capsule. 使用Eloquent ORM,我的数据库连接由Capsule管理。 Should I make the first connection using Singleton method and for any subsequent call in API request, the same thread should be used? 我应该使用Singleton方法进行第一次连接,并且对于API请求中的任何后续调用,应该使用相同的线程吗?

Here is my database connection manager: 这是我的数据库连接管理器:

    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();

What is the best approach to come out of this problem? 解决这个问题的最佳方法是什么?

UPDATE UPDATE

I'm trying another approach for making a persistent connection. 我正在尝试另一种方法来建立持久连接。 But still the persistent connection is not getting closed after the call is done with the job. 但是,在完成对作业的调用之后,持久连接仍未关闭。 Even calling DB::Disconnect is not helping. 即使调用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'));
        }
    });

You should use the same database connection for all queries. 您应该对所有查询使用相同的数据库连接。 The easiest way to do this is to connect to the database in the DI Container as the you can just pull it out again each time you need it. 最简单的方法是连接到DI容器中的数据库,因为您可以在每次需要时再次将其拉出。

Using Slim 3, it looks something like this: 使用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;
};

You can now use in a route callable: 您现在可以在可调用的路由中使用:

$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;

});

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM