How to resolve the error "Base table or view not found"? When I connect my custom module with database external

$database = array(
  'host' =&gt; 'localhost', 
  'database' =&gt; 'DATABASE1',
  'username' =&gt; 'root', 
  'password' =&gt; 'root', 
  'driver' =&gt; 'mysql',
);
Database::addConnectionInfo('mykey', 'default', $database);
   db_set_active('mykey');


it gives error on running the module


  PDOException: SQLSTATE[42S02]: Base table or view not found: 1146
  Table &#039;DATABASE1.filter_format&#039; doesn&#039;t exist: SELECT
  ff.* FROM {filter_format} ff WHERE (status =
  :db_condition_placeholder_0) ORDER BY weight ASC; Array (
  [:db_condition_placeholder_0] =&gt; 1 ) in filter_formats() (line 434
  of /var/www/html/drupalprac/modules/filter/filter.module).
  
  Uncaught exception thrown in shutdown function.
  
  PDOException: SQLSTATE[42S02]: Base table or view not found: 1146
  Table &#039;DATABASE1.semaphore&#039; doesn&#039;t exist: DELETE FROM
  {semaphore} WHERE (value = :db_condition_placeholder_0) ; Array (
  [:db_condition_placeholder_0] =&gt; 1595456215ac3545f2ff1c3.60338979 )
  in lock_release_all() (line 269 of
  /var/www/html/drupalprac/includes/lock.inc). Uncaught exception thrown
  in session handler.
  
  PDOException: SQLSTATE[42S02]: Base table or view not found: 1146
  Table &#039;DATABASE1.sessions&#039; doesn&#039;t exist: SELECT 1 AS
  expression FROM {sessions} sessions WHERE ( (sid =
  :db_condition_placeholder_0) AND (ssid = :db_condition_placeholder_1)
  ); Array ( [:db_condition_placeholder_0] =&gt;
  gSAxKMpfVXzf2EVP4EWBtuF2klcg0RZie30ztTTUjKc
  [:db_condition_placeholder_1] =&gt; ) in _drupal_session_write() (line
  209 of /var/www/html/drupalprac/includes/session.inc).
You need to switch the database back before it carries out any operation that requires the Drupal database.

// Switch to your custom database.
db_set_active('mykey');

// Work with that database (select/insert/update).
...

// Switch back, before you use the Drupal API again
db_set_active();