- 🇮🇳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.