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

Additiona read or write connection opened on reconnect with DB::disconnect() #30957

Closed
bernardwiesner opened this issue Dec 27, 2019 · 7 comments
Labels

Comments

@bernardwiesner
Copy link
Contributor

  • Laravel Version: 6.2
  • PHP Version: 7.2
  • Database Driver & Version: MySQL

Description:

  • When using read and write connections, calling DB::disconnect() and then executing a select query opens a connection to both read and write DB instead of just read. The same happens other way around, if you execute a write query after DB::disconnect() it opens both write and read instead of only write.

  • DB::disconnect() sets both readPdo and pdo to null. In DatabaseManager.php reconnect() calls refreshPdoConnections() that unnecessarily opens both read and write connections to the DB.

  • Using DB::purge() instead solves the problem scrapping the connection and not using the reconnect() when executing a new query.

I could use purge instead but I wonder if there is any downside to it. Seems like disconnect() and reconnect() are opening an unnecessary connection. This causes problems since it can potentially stop both write and read DB in situations when there are too many connections.

Steps To Reproduce:

Set up both a read and write DB in your database.php

Without disconnect

$user = DB::table('users')->first();
sleep(15); 

show full processlist on mysql returns 1 open connection on read and 0 on write for the app.

With disconnect

$user = DB::table('users')->first();
DB::disconnect();
$user = DB::table('users')->first();
sleep(15); 

show full processlist on mysql returns 1 open connection on read and 1 on write for the app.

@driesvints
Copy link
Member

I do wonder how your connections are set up. disconnect on the DatabaseManager only disconnects the default connection. How are your connections named?

@bernardwiesner
Copy link
Contributor Author

bernardwiesner commented Dec 31, 2019

I followed the official documentation for read and write configurations.

https://laravel.com/docs/6.x/database#read-and-write-connections

So basically my connections are named like this

'mysql' => [
    'read' => [
        'host' => [
            '192.168.1.1',
        ],
    ],
    'write' => [
        'host' => [
            '196.168.1.3',
         ],
    ],
    ...
]

I also tried using disconnect('mysql), with the same result.

@themsaid
Copy link
Member

themsaid commented Jan 2, 2020

I can verify the issue and opened a PR with a proposed fix in #30998

@driesvints
Copy link
Member

@bernardwiesner you're linking to the 4.2 docs btw.

@bernardwiesner
Copy link
Contributor Author

@driesvints
Sorry about that, was searching on my phone and that's what came up first, updated it with 6.x.

@driesvints
Copy link
Member

Pr was merged

@bernardwiesner
Copy link
Contributor Author

Cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants