I am using Laravel 5
with php 5.5.9-1ubuntu4.14
and mysql 5.5.47
. I have a multi-tenant environment with each tenant having a separate DB. So for every HTTP request, I set the appropriate DB connection in the BeforeMiddleware
like so
public function handle($request, Closure $next)
{
...
// Set the client DB name and fire it up as the new default DB connection
Config::set('database.connections.mysql_client.host', $db_host);
Config::set('database.connections.mysql_client.database', $db_database);
Config::set('database.connections.mysql_client.username', $db_username);
Config::set('database.connections.mysql_client.password', $db_password);
DB::setDefaultConnection('mysql_client');
...
}
This works correctly even for concurrent HTTP requests.
But now I want to add scheduled jobs to my application. These jobs will send notifications to users belonging to all tenants. So, I again need to connect to respective tenant Databases. But this connection won't be through an HTTP request. Let's say I have a CommunicationController
and a function inside it for sending notifications.
public function sendNotification()
{
...
DB::purge('mysql_client');//IMP
// Set the client DB name and fire it up as the new default DB connection
Config::set('database.connections.mysql_client.host', $db_host);
Config::set('database.connections.mysql_client.database', $db_database);
Config::set('database.connections.mysql_client.username', $db_username);
Config::set('database.connections.mysql_client.password', $db_password);
DB::setDefaultConnection('mysql_client');
...
}
This is where I set the Config
values. This function will be executed every minute through Laravel Scheduler
.
My questions are:
- What will happen when an HTTP request comes in? Will there be any concurrency issues as
Config
parameters are being set in both, HTTP request as well as Scheduled job? - Will the HTTP request connect to some incorrect DB if it's running simultaneously with the scheduled job?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire