function Database::closeConnection

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

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

Parameters

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

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

14 calls to Database::closeConnection()
ConnectionFailureTest::testConnectionFailureLogging in core/modules/dblog/tests/src/Kernel/ConnectionFailureTest.php
Tests logging of connection failures.
ConnectionTest::testConnectionClosing in core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
Tests the closing of a database connection.
ConnectionUnitTest::testOpenClose in core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php
Tests Database::closeConnection() without query.
ConnectionUnitTest::testOpenQueryClose in core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php
Tests Database::closeConnection() with a query.
ConnectionUnitTest::testOpenQueryPrefetchClose in core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php
Tests Database::closeConnection() with a query and custom prefetch method.

... See full list

File

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

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function closeConnection($target = NULL, $key = NULL) {
    // Gets the active connection by default.
    if (!isset($key)) {
        $key = self::$activeKey;
    }
    // To close a connection, it needs to be set to NULL and removed from the
    // static variable. In all cases, closeConnection() might be called for a
    // connection that was not opened yet, in which case the key is not defined
    // yet and we just ensure that the connection key is undefined.
    if (isset($target)) {
        if (isset(self::$connections[$key][$target])) {
            self::$connections[$key][$target]->destroy();
            self::$connections[$key][$target] = NULL;
        }
        unset(self::$connections[$key][$target]);
    }
    else {
        if (isset(self::$connections[$key])) {
            foreach (self::$connections[$key] as $target => $connection) {
                self::$connections[$key][$target]->destroy();
                self::$connections[$key][$target] = NULL;
            }
        }
        unset(self::$connections[$key]);
    }
}

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