Skip to content

[5.3] Deferring write connection doesn't work #15359

Closed
@tomgrohl

Description

@tomgrohl
  • Laravel Version: 5.3.11
  • PHP Version: 7.0.10
  • Database Driver & Version: MariaDB 5.5.44

Description:

Steps To Reproduce:

  1. Setup mysql config as a read/write, you'll notice the write connection is wrong. This is deliberate:
        'mysql' => [
            'driver' => 'mysql',

            'read' => array(
                'host' => 'localhost',
            ),
            'write' => array(
                'host' => '127.0.0.0'
            ),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'laravel'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
  1. Do a find query against a model, .e.g:
User::find(1);

An exception is thrown because it can't connect to the write database. But as I'm not doing a query against the write database I shouldn't get this error.

Below is the stack trace I get:

1. in Connector.php line 119
2. at PDO->__construct('mysql:host=127.0.0.0;port=3306;dbname=laravel', 'laravel_user', 'password', array('0', '2', '0', false, false)) in Connector.php line 119
3. at Connector->createPdoConnection('mysql:host=127.0.0.0;port=3306;dbname=laravel', 'laravel_user', 'password', array('0', '2', '0', false, false)) in Connector.php line 56
4. at Connector->createConnection('mysql:host=127.0.0.0;port=3306;dbname=laravel', array('driver' => 'mysql', 'port' => '3306', 'database' => 'laravel', 'username' => 'laravel_user', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, 'name' => 'mysql', 'host' => '127.0.0.0'), array('0', '2', '0', false, false)) in MySqlConnector.php line 24
5. at MySqlConnector->connect(array('driver' => 'mysql', 'port' => '3306', 'database' => 'laravel', 'username' => 'laravel_user', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, 'name' => 'mysql', 'host' => '127.0.0.0')) in ConnectionFactory.php line 100
6. at ConnectionFactory->Illuminate\Database\Connectors\{closure}()
7. at call_user_func(object(Closure)) in Connection.php line 956
8. at Connection->getPdo() in Connection.php line 824
9. at Connection->reconnectIfMissingConnection() in Connection.php line 709
10. at Connection->run('select * from `users` where `users`.`id` = ? limit 1', array('1'), object(Closure)) in Connection.php line 351
11. at Connection->select('select * from `users` where `users`.`id` = ? limit 1', array('1'), true) in Builder.php line 1645

It looks like this is the code causing the problem is class Illuminate\Database\Connection:

    /**
     * Reconnect to the database if a PDO connection is missing.
     *
     * @return void
     */
    protected function reconnectIfMissingConnection()
    {
        if (is_null($this->getPdo()) || is_null($this->getReadPdo())) {
            $this->reconnect();
        }
    }

I understand it reconnecting but I would have thought it makes more sense to do it on connections that are needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions