function Database::closeConnection

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::closeConnection()
  2. 10 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::closeConnection()
  3. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::closeConnection()
  4. 8.9.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::closeConnection()
  5. 7.x includes/database/database.inc \Database::closeConnection()

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

Parameters

?string $target: (optional) The database target name. Defaults to NULL meaning that all target connections will be closed.

?string $key: (optional) The database connection key. Defaults to NULL which means the active key.

23 calls to Database::closeConnection()
ConnectionFailureTest::testConnectionFailureLogging in core/modules/dblog/tests/src/Kernel/ConnectionFailureTest.php
Tests logging of connection failures.
ConnectionTest::testClientEncoding in core/modules/pgsql/tests/src/Kernel/pgsql/ConnectionTest.php
Tests the client encoding is set to UTF8 when the connection is opened.
ConnectionTest::testConnectionClosing in core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
Tests the closing of a database connection.
ConnectionTest::testConnectionWithIntegerKeyAndTarget in core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
Tests deprecation of integer values for connection $key and $target.
Database::removeConnection in core/lib/Drupal/Core/Database/Database.php
Remove a connection and its corresponding connection information.

... See full list

File

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

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function closeConnection(string|int|null $target = NULL, string|int|null $key = NULL) : void {
  if ($target !== NULL && is_int($target)) {
    @trigger_error('Passing an integer value to the $target parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
  }
  if ($key !== NULL && is_int($key)) {
    @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
  }
  // Gets the active connection by default.
  if (!isset($key)) {
    $key = self::$activeKey;
  }
  if (isset($target) && isset(self::$connections[$key][$target])) {
    // @phpstan-ignore method.deprecated
    self::$connections[$key][$target]->commitAll();
    unset(self::$connections[$key][$target]);
  }
  elseif (isset(self::$connections[$key])) {
    foreach (self::$connections[$key] as $connection) {
      // @phpstan-ignore method.deprecated
      $connection->commitAll();
    }
    unset(self::$connections[$key]);
  }
  // When last connection for $key is closed, we also stop any active
  // logging.
  if (empty(self::$connections[$key])) {
    unset(self::$logs[$key]);
  }
  // Force garbage collection to run. This ensures that client connection
  // objects and results in the connection being closed are destroyed.
  gc_collect_cycles();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.