1

I have 2 databases, the first one with Drupal and the second one with external data. In a controller I want to read data from that second database and display it.

As the external database can be available or not, is there any method to check if the connection is available or not?

My code is working, but if the DB is not available (or unset) I get the following error:

Drupal\Core\Database\ConnectionNotDefinedException: The specified database connection is not defined: schedules in Drupal\Core\Database\Database::openConnection() (line 364 of core/lib/Drupal/Core/Database/Database.php).

Code:

abstract class AbstractSchedulesRepository {

  /**
   * Schedules repository target.
   */
  public const SCHEDULES_REPOSITORY_TARGET_DATABASE = 'default';

  /**
   * Schedules repository key.
   */
  public const SCHEDULES_REPOSITORY_KEY_DATABASE = 'schedules';

  /**
   * Drupal\schedules\Repository\SchedulesCache definition.
   *
   * @var \Drupal\schedules\Cache\SchedulesCache
   */
  protected $schedulesCache;

  /**
   * Drupal\Core\Language\LanguageManager definition.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected $languageManager;

  /**
   * Drupal\Core\Database\Connection definition.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructor.
   *
   * @param \Drupal\schedules\Cache\SchedulesCache $schedulesCache
   *   Schedules cache.
   * @param \Drupal\Core\Language\LanguageManager $languageManager
   *   Language manager.
   */
  public function __construct(SchedulesCache $schedulesCache, LanguageManager $languageManager) {
    $this->schedulesCache  = $schedulesCache;
    $this->languageManager = $languageManager;
    $this->connection      = Database::getConnection(self::SCHEDULES_REPOSITORY_TARGET_DATABASE, self::SCHEDULES_REPOSITORY_KEY_DATABASE);
  }
4
  • 1
    Why not just catch the ConnectionNotDefinedException?
    – Clive
    Feb 15, 2021 at 10:53
  • Thank you for your response. Yes, you are right, but I find it odd that Drupal has no method to check if the connection is available.
    – Javier Rey
    Feb 15, 2021 at 10:58
  • 2
    There’s no explicit method but It does give you a good mechanism instead - it throws exceptions if it’s not available. This is much more useful than a dedicated method, as different exceptions can be thrown to give the catcher context for why, as opposed to just yes or no. Also a connectionAvailable or similar method would have to actually make a connection, which costs time. As you’re already planning to make a connection, that’s wasted time. Better that you just make the connection and observe the result than cost yourself two connections by checking up front
    – Clive
    Feb 15, 2021 at 12:09
  • I understand, I will check the exception, thank you very much for your answer, it helped me a lot :)
    – Javier Rey
    Feb 15, 2021 at 12:56

1 Answer 1

1

is there any method to check if the connection is available or not?

If you mean class method, then no, because it wouldn’t be necessary. You can simply catch the exception, which will tell you immediately that the connection was not successful:

try {
  // Code using a database connection
} catch (ConnectionNotDefinedException $e) {
  // No database for you
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.