5

I need to connect many databases dynamically in laravel app.
How to set database connection pool?

for example,there are many second-class domain name,like this:

chicago.example.com
newyork.example.com
losangeles.example.com
...

They have separate database:

chicago
newyork
losangeles
...

I connect these databases dynamically like this:

public function store(Request $request)
{
    //post request from http://chicago.example.com/articles
    $server_name_arr=explode('.',$_SERVER['SERVER_NAME']); //the result is ['chicago','example','com']
    $db=array_slice($server_name_arr,-3,1)[0]; //the result is 'chicago'

    Config::set('database.connections.mysql.database', $db);
    DB::reconnect('mysql');

    //...
}

For performance,I want to set database connection pool,how to do it in laravel?

zwl1619
  • 3,572
  • 12
  • 44
  • 100
  • Any reason why you don't just have three different instances of your code for each location and just connect to the preferred database for each location? Changing the default database connection on the fly just seems problematic to me. – Devon May 07 '18 at 14:27
  • redis https://redis.io/topics/introduction / https://laravel.com/docs/5.6/redis#configuration . Check this as well https://laracasts.com/discuss/channels/eloquent/laravel-5-multiple-database-connection.As far as I know multiple db/failover db are n/a: https://github.com/laravel/framework/issues/2808 – Indra May 07 '18 at 14:36
  • For performance you use cache, in order to be 99.999% up you use db-pool / failover. – Kyslik May 07 '18 at 14:48
  • You need to see this : [https://stackoverflow.com/questions/39753/connection-pooling-in-php](https://stackoverflow.com/questions/39753/connection-pooling-in-php) – rrCKrr Jan 06 '19 at 15:16

0 Answers0