Skip navigation


7 final public static Database::getConnection($target = 'default', $key = NULL)

Gets the connection object for the specified database key and target.

Note: do not use the setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE) on the returned object because of


$target: The database target name.

$key: The database connection key. Defaults to NULL which means the active key.

Return value

DatabaseConnection The corresponding connection object.

62 calls to Database::getConnection()


drupal/includes/database/, line 1440
Core systems for the database layer.


final public static function getConnection($target = 'default', $key = NULL) {
  if (!isset($key)) {
    // By default, we want the active connection, set in setActiveConnection.
    $key = self::$activeKey;
  // If the requested target does not exist, or if it is ignored, we fall back
  // to the default target. The target is typically either "default" or
  // "slave", indicating to use a slave SQL server if one is available. If
  // it's not available, then the default/master server is the correct server
  // to use.
  if (!empty(self::$ignoreTargets[$key][$target]) || !isset(self::$databaseInfo[$key][$target])) {
    $target = 'default';

  if (!isset(self::$connections[$key][$target])) {
    // If necessary, a new connection is opened.
    self::$connections[$key][$target] = self::openConnection($key, $target);
  return self::$connections[$key][$target];