You are here

final public static function Database::getConnection in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::getConnection()

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

Parameters

string $target: The database target name.

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

Return value

\Drupal\Core\Database\Connection The corresponding connection object.

161 calls to Database::getConnection()
addTourLegacyConfig.php in core/modules/tour/tests/fixtures/addTourLegacyConfig.php
Adds deprecated tour config for testing updates.
BigPipeTest::testBigPipe in core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
Tests BigPipe-delivered HTML responses when JavaScript is enabled.
BlockContentCreationTest::testFailedBlockCreation in core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php
Verifies that a transaction rolls back the failed creation.
BootstrapConfigStorageFactory::getDatabaseStorage in core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php
Returns a Database configuration storage implementation.
BrokenCacheUpdateTest::testUpdate in core/modules/system/tests/src/Functional/UpdateSystem/BrokenCacheUpdateTest.php
Ensures that a broken or out-of-date element info cache is not used.

... See full list

1 string reference to 'Database::getConnection'
core.services.yml in core/core.services.yml
core/core.services.yml

File

core/lib/Drupal/Core/Database/Database.php, line 153

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static final 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
  // "replica", indicating to use a replica SQL server if one is available. If
  // it's not available, then the default/primary 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];
}