Add method to check if database connection exists.

Created on 24 May 2022, over 2 years ago
Updated 27 July 2023, over 1 year ago

Problem/Motivation

Currently we can define additional databases in settings.php as below.

// Set the SQLite Database configuration.
$databases['sqlite']['default'] = [
  'driver' => 'sqlite',
  'database' => 'sites/default/files/private/second_db.sqlite',
];

We can establish connection in our code by using

$connection = Database::getConnection('default', 'sqlite');

We don't have any native method in core to check if the database connection is established.

If the database connection is not established a fatal error is being thrown as below.

Drupal\Core\Entity\EntityStorageException:
The specified database connection is not defined: sqlite in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 811 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Even if we use try , catch we are still getting fatal error.

try {
    $connection = Database::getConnection('default', 'sqlite');
}
catch (Drupal\Core\Entity\EntityStorageException $e) {
    // Do something here.
}

Steps to reproduce

Try calling a non-existing database connection. You will get fatal error even while using try, catch.

Proposed resolution

Add a method in core to check if the database connection exists something like hasConnection() to check if the database connection exists.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Active

Version

10.0

Component
Database 

Last updated 2 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇮🇳India bhanu951

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India saranyamariappan

    I have a different case where I have to temporarily connect to a 2nd DB or 3rd DB and make some query request, build an array of options from that Database table and switch back to the current Database. The connection was working perfect on the higher level server(Dev,Staging & Production as they were in AWS) but the same code is throwing error on my lcoal. So I have found a solution to fix that WSOD on local.

    use Drupal\Core\Database\Database;
    use Drupal\Core\Entity\EntityStorageException;
    
    function buildOptionforSelect ($newdbkey) {
    
    if (is_null($newdbkey)) {
        Database::setActiveConnection('DB_3');
      }
      else {
        Database::setActiveConnection($newdbkey);
      }
    try {
       $option = [];
        if (<strong>Database::isActiveConnection()</strong>) { //without this line I faced WSOD on my local.
          $connection = Database::getConnection();
          if ($connection->getKey() != 'default') {
            //Build something for $option = [];
          }
       }
    }
    catch (EntityStorageException $e) {
        \Drupal::logger('DBHandshake')->error($e->getMessage());
      }
    Database::setActiveConnection();
    return $options;
    }

    Hope this might be helpful for other cases.

Production build 0.71.5 2024