mercredi 5 décembre 2018

How Laravel maintains persistent database connection

Background

I was developing a Laravel application in LAMP setup at my local machine( my laptop).

For testing purpose, I tried to use the mysql service of Aws RDS instead my local mysql server. Found that, a api call which have only one db call with no join (query: show tables) - takes on average 12s. This is ridiculous. When I am using local mysql server, it is around than 600ms. Till now, PDO::ATTR_PERSISTANT was not enabled.

Logging in Illuminate\Database\Connectors\Connector.php [ inside createConnection() method ], I found that, this method is called for each request. This is for both mysql server.

Then, I set PDO::ATTR_PERSISTANT to true. But response time's are similar.

After looking a more closer, found this in the same file:

/**
 * Create a new PDO connection instance.
 *
 * @param  string  $dsn
 * @param  string  $username
 * @param  string  $password
 * @param  array  $options
 * @return \PDO
 */
protected function createPdoConnection($dsn, $username, $password, $options)
{
    if (class_exists(PDOConnection::class) && ! $this->isPersistentConnection($options)) {
        return new PDOConnection($dsn, $username, $password, $options);
    }

    return new PDO($dsn, $username, $password, $options);
}

And PDOConnection - which extends PDO, that is used when persistent is false - constructor is:

public function __construct($dsn, $user = null, $password = null, array $options = null)
{
    try {
        parent::__construct($dsn, $user, $password, $options);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['Doctrine\DBAL\Driver\PDOStatement', []]);
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (\PDOException $exception) {
        throw new PDOException($exception);
    }
}

It seems, setting PDO::ATTR_PERSISTANT to true has no effect. This made me confused.

  1. How actually persistent database connection plays in Laravel?
  2. What was the reason behind long response time (with mysql service of Aws RDS) ?


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire