Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tomgrohl opened this issue Sep 9, 2016 · 6 comments
Closed

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

tomgrohl opened this issue Sep 9, 2016 · 6 comments

Comments

@tomgrohl
Copy link

tomgrohl commented Sep 9, 2016

  • 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.

@tomgrohl tomgrohl changed the title Deferring write connection doesn't work [5.3] Deferring write connection doesn't work Sep 9, 2016
@vaidas-lungis
Copy link

+1

@themsaid
Copy link
Member

Hello @acasar :)
Any feedback on this issue?

@acasar
Copy link
Contributor

acasar commented Sep 27, 2016

@themsaid Wasn't my PR :)

Maybe #15031 by @patrickcarlohickman or #11615 by @stevenmyhre?

@themsaid
Copy link
Member

@acasar Thank you and sorry to bother 😅

@tomgrohl
Copy link
Author

I've updated the version of Laravel mentioned from 5.3.6 to 5.3.11 (current latest) and the issue still happens.

@themsaid
Copy link
Member

fixe din #16422

taylorotwell pushed a commit that referenced this issue Dec 6, 2016
issue #15359
fixed in version 5.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants