Laravel-swoole: Changes to database config are not applied after connection purge and reconnect

Created on 24 May 2018  Â·  11Comments  Â·  Source: swooletw/laravel-swoole

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: [email protected]]
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.

help wanted question

Most helpful comment

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

All 11 comments

Hi @megawubs ,

Is you code in the same request?

Yes, it's being preformed in a middleware

Hi @megawubs ,

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

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

Hi @megawubs ,

Does this middleware work in the traditionally FPM?

Yes

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

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)

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.

I'll try that, thanks

Verstuurd vanaf mijn iPhone

Op 25 mei 2018 om 5:16 PM heeft Albert Chen notifications@github.com het volgende geschreven:

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.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

It works! thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omsun123 picture omsun123  Â·  6Comments

caveman74 picture caveman74  Â·  7Comments

benpoulson picture benpoulson  Â·  3Comments

codercms picture codercms  Â·  5Comments

Abbotton picture Abbotton  Â·  9Comments