final protected static function Database::openConnection

Opens a connection to the server specified by the given key and target.

Parameters

$key: The database connection key, as specified in settings.php. The default is "default".

$target: The database target to open.

Throws

Drupal\Core\Database\ConnectionNotDefinedException

Drupal\Core\Database\DriverNotSpecifiedException

1 call to Database::openConnection()
Database::getConnection in drupal/core/lib/Drupal/Core/Database/Database.php
Gets the connection object for the specified database key and target.

File

drupal/core/lib/Drupal/Core/Database/Database.php, line 362
Definition of Drupal\Core\Database\Database

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

protected static final function openConnection($key, $target) {
  if (empty(self::$databaseInfo)) {
    self::parseConnectionInfo();
  }

  // If the requested database does not exist then it is an unrecoverable
  // error.
  if (!isset(self::$databaseInfo[$key])) {
    throw new ConnectionNotDefinedException('The specified database connection is not defined: ' . $key);
  }
  if (!($driver = self::$databaseInfo[$key][$target]['driver'])) {
    throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
  }
  $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
  $new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
  $new_connection
    ->setTarget($target);
  $new_connection
    ->setKey($key);

  // If we have any active logging objects for this connection key, we need
  // to associate them with the connection we just opened.
  if (!empty(self::$logs[$key])) {
    $new_connection
      ->setLogger(self::$logs[$key]);
  }
  return $new_connection;
}