Laravel 用Voyager导致多只数据库连接总是回到默认连接?

题目及分析

近期的品类遇到一个飞的题目,在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 \Illuminate\Database\Connection                                            
     *                                                                                    
     * @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报了一个称为吧Larapack\DoctrineSupport\DoctrineSupportServiceProvider(也未明了干嘛的,也尚未盼在用),看下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/,修改命名空间啊App\Providers,并将DoctrineSupport相互关行都勾。然后修改下config/app.php,将TCG\Voyager\VoyagerServiceProvider::class修改为App\Providers\VoyagerServiceProvider::class即使化解了。

网站地图xml地图