diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php
index 94a23fb..7d36e79 100644
--- a/core/lib/Drupal/Core/Database/Connection.php
+++ b/core/lib/Drupal/Core/Database/Connection.php
@@ -145,9 +145,9 @@ function __construct($dsn, $username, $password, $driver_options = array()) {
     // Call PDO::__construct and PDO::setAttribute.
     parent::__construct($dsn, $username, $password, $driver_options);
 
-    // Set a specific PDOStatement class if the driver requires that.
+    // Set a Statement class, unless the driver opted out.
     if (!empty($this->statementClass)) {
-      $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this)));
+      $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this->connectionOptions['key'], $this->connectionOptions['target'])));
     }
   }
 
diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php
index 6a425de..9fb5cc5 100644
--- a/core/lib/Drupal/Core/Database/Database.php
+++ b/core/lib/Drupal/Core/Database/Database.php
@@ -338,8 +338,8 @@ public static function addConnectionInfo($key, $target, $info) {
    */
   final public static function removeConnection($key) {
     if (isset(self::$databaseInfo[$key])) {
+      self::closeConnection(NULL, $key);
       unset(self::$databaseInfo[$key]);
-      unset(self::$connections[$key]);
       return TRUE;
     }
     else {
@@ -374,8 +374,14 @@ public static function addConnectionInfo($key, $target, $info) {
       throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
     }
 
+    // Allow the database connection key and target to be passed forward by the
+    // constructor.
+    $info = self::$databaseInfo[$key][$target];
+    $info['key'] = $key;
+    $info['target'] = $target;
+
     $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
-    $new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
+    $new_connection = new $driver_class($info);
     $new_connection->setTarget($target);
     $new_connection->setKey($key);
 
@@ -402,11 +408,16 @@ public static function closeConnection($target = NULL, $key = NULL) {
     if (!isset($key)) {
       $key = self::$activeKey;
     }
-    // To close the connection, we need to unset the static variable.
+    // To close a connection, it needs to be set to NULL and removed from the
+    // static variable.
     if (isset($target)) {
+      self::$connections[$key][$target] = NULL;
       unset(self::$connections[$key][$target]);
     }
     else {
+      foreach (self::$connections[$key] as $target => $connection) {
+        $connection = NULL;
+      }
       unset(self::$connections[$key]);
     }
   }
diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
index fbbff42..53b1415 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
@@ -24,11 +24,11 @@
 class Connection extends DatabaseConnection {
 
   /**
-   * Flag to indicate if we have registered the nextID cleanup function.
+   * Flag to indicate if the cleanup function in __destruct() should run.
    *
    * @var boolean
    */
-  protected $shutdownRegistered = FALSE;
+  protected $needsCleanup = FALSE;
 
   public function __construct(array $connection_options = array()) {
     // This driver defaults to transaction support, except if explicitly passed FALSE.
@@ -89,6 +89,13 @@ public function __construct(array $connection_options = array()) {
     $this->exec(implode('; ', $connection_options['init_commands']));
   }
 
+  public function __destruct() {
+    if ($this->needsCleanup) {
+      $this->nextIdDelete();
+    }
+    parent::__destruct();
+  }
+
   public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
     return $this->query($query . ' LIMIT ' . (int) $from . ', ' . (int) $count, $args, $options);
   }
@@ -126,12 +133,7 @@ public function nextId($existing_id = 0) {
       $this->query('INSERT INTO {sequences} (value) VALUES (:value) ON DUPLICATE KEY UPDATE value = value', array(':value' => $existing_id));
       $new_id = $this->query('INSERT INTO {sequences} () VALUES ()', array(), array('return' => Database::RETURN_INSERT_ID));
     }
-    if (!$this->shutdownRegistered) {
-      // Use register_shutdown_function() here to keep the database system
-      // independent of Drupal.
-      register_shutdown_function(array($this, 'nextIdDelete'));
-      $shutdownRegistered = TRUE;
-    }
+    $this->needsCleanup = TRUE;
     return $new_id;
   }
 
diff --git a/core/lib/Drupal/Core/Database/Statement.php b/core/lib/Drupal/Core/Database/Statement.php
index c5b1735..9972679 100644
--- a/core/lib/Drupal/Core/Database/Statement.php
+++ b/core/lib/Drupal/Core/Database/Statement.php
@@ -24,6 +24,20 @@
 class Statement extends PDOStatement implements StatementInterface {
 
   /**
+   * The database connection key for this statement.
+   *
+   * @var string
+   */
+  protected $key;
+
+  /**
+   * The database connection target for this statement.
+   *
+   * @var string
+   */
+  protected $target;
+
+  /**
    * Reference to the database connection object for this statement.
    *
    * The name $dbh is inherited from PDOStatement.
@@ -32,8 +46,9 @@ class Statement extends PDOStatement implements StatementInterface {
    */
   public $dbh;
 
-  protected function __construct($dbh) {
-    $this->dbh = $dbh;
+  protected function __construct($key, $target) {
+    $this->key = $key;
+    $this->target = $target;
     $this->setFetchMode(PDO::FETCH_OBJ);
   }
 
@@ -50,7 +65,7 @@ public function execute($args = array(), $options = array()) {
       }
     }
 
-    $logger = $this->dbh->getLogger();
+    $logger = Database::getConnection($this->target, $this->key)->getLogger();
     if (!empty($logger)) {
       $query_start = microtime(TRUE);
     }
