问题与分析
最近的项目碰到一个奇怪的问题,在Laravel(5.3)中想建立多个数据库连接连到MySQL的不同数据库(另一个连接名为conn2
),执行如下语句得到却发现得到的仍然是默认连接:
$conn2 = DB::connection("conn2");
百思不得其解,只好去啃源码,最后定位到vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php
文件的createConnection
这个函数,源码如下:
/**
* Create a new connection instance.
*
* @param string $driver
* @param PDO|Closure $connection
* @param string $database
* @param string $prefix
* @param array $config
* @return IlluminateDatabaseConnection
*
* @throws InvalidArgumentException
*/
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
{
//以mysql为例,下面这句的意思就是如果检测到名为db.connection.mysql的Container,那么就返回该Container。测试发现总是进入该分支,也就是说明某个模块注册了该名称的Container以致不能创建多个连接。
if ($this->container->bound($key = "db.connection.{$driver}")) {
return $this->container->make($key, [$connection, $database, $prefix, $config]);
}
switch ($driver) {
case 'mysql':
return new MySqlConnection($connection, $database, $prefix, $config);
case 'pgsql':
return new PostgresConnection($connection, $database, $prefix, $config);
case 'sqlite':
return new SQLiteConnection($connection, $database, $prefix, $config);
case 'sqlsrv':
return new SqlServerConnection($connection, $database, $prefix, $config);
}
throw new InvalidArgumentException("Unsupported driver [$driver]");
}
问题就出在第一句的判断上,测试发现总是能进入分支,说明某个模块注册了提供了db.connection.mysql
的Container。最后查到是由于安装了第三方包Voyager
引起的。
Voyager
注册了一个名为LarapackDoctrineSupportDoctrineSupportServiceProvider
(也不知道干嘛的,也没看到在使用),看下DoctrineSupportServiceProvider
的源码,可以找到确实注册了db.connection.mysql
的单例Container
。
/**
* Register MySQL database connection.
*/
protected function registerMySqlDatabaseConnection()
{
$this->app->singleton('db.connection.mysql', function ($app, $parameters) {
// First, we list the passes parameters into single
// variables. I do this because it is far easier
// to read than using it as eg $parameters[0].
list($connection, $database, $prefix, $config) = $parameters;
// Next we can initialize the connection.
$connection = new MySqlConnection($connection, $database, $prefix, $config);
// Add Doctrine types for better support
$this->addDoctrineTypes($connection);
$connection->setSchemaGrammar(new MySqlGrammar());
return $connection;
});
}
解决方法
将vendor/tcg/voyager/src/VoyagerServiceProvider.php
复制一份到app/Providers/
,修改命名空间为AppProviders
,并将DoctrineSupport
相关行都删除。然后修改下config/app.php
,将TCGVoyagerVoyagerServiceProvider::class
修改为AppProvidersVoyagerServiceProvider::class
就解决了。