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

Changes to database config are not applied after connection purge and reconnect #64

Closed
megawubs opened this issue May 24, 2018 · 11 comments

Comments

@megawubs
Copy link

Please answer these questions before submitting your issue. Thanks!

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole)
    PHP 7.2.5 (cli) (built: Apr 26 2018 12:07:32) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.5, Copyright (c) 1999-2018, by Zend Technologies

swoole

swoole support => enabled
Version => 1.10.5
Author => tianfeng.han[email: mikan.tenny@gmail.com]
kqueue => enabled
rwlock => enabled
async http/websocket client => enabled
pcre => enabled
zlib => enabled

Directive => Local Value => Master Value
swoole.aio_thread_num => 2 => 2
swoole.display_errors => On => On
swoole.use_namespace => Off => Off
swoole.fast_serialize => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

  1. Please provide your Laravel/Lumen version.
    Laravel v5.5.40

  2. Which release version of this package are you using?
    v2.3.8

  3. What did you do? If possible, provide a recipe for reproducing the error.

        $db = app('db');
        $db->purge('mysql_database_1');
        $db->purge('mysql_database_2');

        $config->set('database.connections.mysql_database_1.database', 'database_5');
        $config->set('database.connections.mysql_database_2.database', 'database_6');

        $db->reconnect('mysql_database_1');
        $db->reconnect('mysql_database_2');
  1. What did you expect to see?
    my newly configured database to be used.

  2. What did you see instead?
    The old config is still used when recreating the database connection.

@megawubs megawubs changed the title Changes to database config is not applied after connection purge and reconnect Changes to database config are not applied after connection purge and reconnect May 24, 2018
@albertcht
Copy link
Member

Hi @megawubs ,

Is you code in the same request?

@megawubs
Copy link
Author

Yes, it's being preformed in a middleware

@albertcht
Copy link
Member

albertcht commented May 24, 2018

Hi @megawubs ,

Where did you define your $config ? How about using config()->set('foo', 'bar') and see if config changes by config('foo')?

@megawubs
Copy link
Author

ah, i see i missed that part from copy pasting.

The full code is:

$config = app('config');
 $db = app('db');
        $db->purge('mysql_database_1');
        $db->purge('mysql_database_2');

        $config->set('database.connections.mysql_database_1.database', 'database_5');
        $config->set('database.connections.mysql_database_2.database', 'database_6');

        $db->reconnect('mysql_database_1');
        $db->reconnect('mysql_database_2');

i've tried it with different ways of resolving the config. That's not the problem. When i read out the config later, it is changed. Only, the database connection is not...

@albertcht
Copy link
Member

Hi @megawubs ,

Does this middleware work in the traditionally FPM?

@megawubs
Copy link
Author

Yes

@albertcht
Copy link
Member

albertcht commented May 24, 2018

Try add db to instances or add Illuminate\Database\DatabaseServiceProvider::class to providers in config/swoole_http.php.

@megawubs
Copy link
Author

Your tip to add db to the instances field helps a bit. But only for DB::* calls, not for eloquent calls.

I've created a test application to be able to isolate the problem. You can see it here: https://github.com/megawubs/swoole-debug

The steps to reproduce are located in the readme.

When i add this: \Log::info('the connection is: ', (array)$this->connection); on line 2588 in Illuminate\Database\Query\Builder you can see that the connection does not change when using eloquent. (route /1)

@albertcht
Copy link
Member

Hi @megawubs ,

I didn't clone your repo yet, but I think it is caused by Illuminate\Database\Eloquent\Model::class. There are lots of statics in this model class. ConnectionResolver is also called by static and it's only set once in service provider. This may cause static variable pollutions.

Did you try to add Illuminate\Database\DatabaseServiceProvider::class to providers in config/swoole_http.php? Not sure if it helps.

@megawubs
Copy link
Author

megawubs commented May 25, 2018 via email

@megawubs
Copy link
Author

It works! thank you!

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

No branches or pull requests

2 participants