diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php
index 59b9eb7e83fd793b704f7477c1d79881585b32d8..92c9fc2a8c9465b3108dd1eab0c3e604692c7e88 100644
--- a/core/modules/views/src/Plugin/views/query/Sql.php
+++ b/core/modules/views/src/Plugin/views/query/Sql.php
@@ -1248,6 +1248,24 @@ protected function compileFields($query) {
     }
   }
 
+  /**
+   * Gets the database connection to use for the view.
+   *
+   * The returned database connection does not have to be the default database
+   * connection. It can also be to another database connection when the view is
+   * to an external database or a replica database.
+   *
+   * @return \Drupal\Core\Database\Connection
+   *   The database connection to be used for the query.
+   */
+  public function getConnection() {
+    // Set the replica target if the replica option is set for the view.
+    $target = empty($this->options['replica']) ? 'default' : 'replica';
+    // Use an external database when the view configured to.
+    $key = $this->view->base_database ?? 'default';
+    return Database::getConnection($target, $key);
+  }
+
   /**
    * Generate a query and a countquery from all of the information supplied
    * to the object.
@@ -1282,22 +1300,9 @@ public function query($get_count = FALSE) {
       $this->getCountOptimized = TRUE;
     }
 
-    $options = [];
-    $target = 'default';
-    $key = 'default';
-    // Detect an external database and set the
-    if (isset($this->view->base_database)) {
-      $key = $this->view->base_database;
-    }
-
-    // Set the replica target if the replica option is set
-    if (!empty($this->options['replica'])) {
-      $target = 'replica';
-    }
-
     // Go ahead and build the query.
-    $query = Database::getConnection($target, $key)
-      ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options)
+    $query = $this->getConnection()
+      ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'))
       ->addTag('views')
       ->addTag('views_' . $this->view->storage->id());
 
diff --git a/core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php b/core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php
index 6846f77630462c4032c9424600f37dfec2320df1..8375fdae207797818f479eee80c923b76c6843f8 100644
--- a/core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php
+++ b/core/modules/views/tests/src/Kernel/Plugin/SqlQueryTest.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\Tests\views\Kernel\Plugin;
 
+use Drupal\Core\Database\Database;
 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
 use Drupal\views\Views;
 
@@ -79,4 +80,51 @@ public function testExecuteMetadata() {
     }
   }
 
+  /**
+   * Tests the method \Drupal\views\Plugin\views\query\Sql::getConnection().
+   *
+   * @covers \Drupal\views\Plugin\views\query\Sql::getConnection
+   *
+   * This needs to be a kernel test because the tested method uses the method
+   * \Drupal\Core\Database\Database::getConnection() which is a 'final' method
+   * and therefore cannot be mocked.
+   */
+  public function testGetConnection() {
+    $view = Views::getView('test_view');
+    $view->setDisplay();
+
+    // Add 3 database connections for the different options that the method
+    // getConnection() supports.
+    $connection_info = Database::getConnectionInfo('default');
+    Database::addConnectionInfo('default', 'replica', $connection_info['default']);
+    Database::addConnectionInfo('corefake', 'default', $connection_info['default']);
+    Database::addConnectionInfo('corefake', 'replica', $connection_info['default']);
+
+    // Test the database connection with no special options set.
+    $this->assertSame('default', $view->getQuery()->getConnection()->getKey());
+    $this->assertSame('default', $view->getQuery()->getConnection()->getTarget());
+
+    // Test the database connection with the option 'replica' set to TRUE;
+    $view->getQuery()->options['replica'] = TRUE;
+    $this->assertSame('default', $view->getQuery()->getConnection()->getKey());
+    $this->assertSame('replica', $view->getQuery()->getConnection()->getTarget());
+
+    // Test the database connection with the view 'base_database' set.
+    $view->getQuery()->options['replica'] = FALSE;
+    $view->base_database = 'corefake';
+    $this->assertSame('corefake', $view->getQuery()->getConnection()->getKey());
+    $this->assertSame('default', $view->getQuery()->getConnection()->getTarget());
+
+    // Test the database connection with the view 'base_database' set and the
+    // option 'replica' set to TRUE.
+    $view->getQuery()->options['replica'] = TRUE;
+    $this->assertSame('corefake', $view->getQuery()->getConnection()->getKey());
+    $this->assertSame('replica', $view->getQuery()->getConnection()->getTarget());
+
+    // Clean up the created database connections.
+    Database::closeConnection('replica', 'default');
+    Database::closeConnection('default', 'corefake');
+    Database::closeConnection('replica', 'corefake');
+  }
+
 }