Index: includes/database/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/database.inc,v
retrieving revision 1.145
diff -u -p -r1.145 database.inc
--- includes/database/database.inc	30 Nov 2010 07:16:24 -0000	1.145
+++ includes/database/database.inc	11 Dec 2010 20:43:39 -0000
@@ -194,6 +194,13 @@ abstract class DatabaseConnection extend
   protected $target = NULL;
 
   /**
+   * The key representing this connection.
+   *
+   * @var string
+   */
+  protected $key = NULL;
+
+  /**
    * The current database logging object for this connection.
    *
    * @var DatabaseLog
@@ -470,6 +477,28 @@ abstract class DatabaseConnection extend
   }
 
   /**
+   * Tells this connection object what its key is.
+   *
+   * @param $target
+   *   The key this connection is for.
+   */
+  public function setKey($key) {
+    if (!isset($this->key)) {
+      $this->key = $key;
+    }
+  }
+
+  /**
+   * Returns the key this connection is associated with.
+   *
+   * @return
+   *   The key of this connection.
+   */
+  public function getKey() {
+    return $this->key;
+  }
+
+  /**
    * Associates a logging object with this connection.
    *
    * @param $logger
@@ -1539,6 +1568,7 @@ abstract class Database {
     require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
     $new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
     $new_connection->setTarget($target);
+    $new_connection->setKey($key);
 
     // If we have any active logging objects for this connection key, we need
     // to associate them with the connection we just opened.
Index: includes/database/query.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/query.inc,v
retrieving revision 1.58
diff -u -p -r1.58 query.inc
--- includes/database/query.inc	26 Oct 2010 15:26:09 -0000	1.58
+++ includes/database/query.inc	11 Dec 2010 20:43:39 -0000
@@ -242,6 +242,20 @@ abstract class Query implements QueryPla
   protected $connection;
 
   /**
+   * The target of the connection object.
+   * 
+   * @var string
+   */
+  protected $connectionTarget;
+
+  /**
+   * The key of the connection object.
+   * 
+   * @var string
+   */
+  protected $connectionKey;
+
+  /**
    * The query options to pass on to the connection object.
    *
    * @var array
@@ -270,10 +284,29 @@ abstract class Query implements QueryPla
    */
   public function __construct(DatabaseConnection $connection, $options) {
     $this->connection = $connection;
+    $this->connectionKey = $this->connection->getKey();
+    $this->connectionTarget = $this->connection->getTarget();
+
     $this->queryOptions = $options;
   }
 
   /**
+   * Implements the magic __sleep function to disconnects from the database.
+   */
+  public function __sleep() {
+    $keys = get_object_vars($this);
+    unset($keys['connection']);
+    return array_keys($keys);
+  }
+
+  /**
+  * Implements the magic __wakeup function to reconnect to the database.
+   */
+  public function __wakeup() {
+    $this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
+  }
+
+  /**
    * Runs the query against the database.
    */
   abstract protected function execute();
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.104
diff -u -p -r1.104 database_test.test
--- modules/simpletest/tests/database_test.test	15 Oct 2010 04:34:15 -0000	1.104
+++ modules/simpletest/tests/database_test.test	11 Dec 2010 20:43:39 -0000
@@ -2751,6 +2751,33 @@ class DatabaseLoggingTestCase extends Da
 }
 
 /**
+ * Query serialization tests.
+ */
+class DatabaseSerializeQueryTestCase extends DatabaseTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Serialize query',
+      'description' => 'Test serializing and unserializing a query.',
+      'group' => 'Database',
+    );
+  }
+
+  /**
+   * Confirm that a query can be serialized and unserialized.
+   */
+  function testSerializeQuery() {
+    $query = db_select('test');
+    $query->addField('test', 'age');
+    $query->condition('name', 'Ringo');
+    // If this doesn't work, it will throw an exception, so no need for an
+    // assertion.
+    $query = unserialize(serialize($query));
+    $results = $query->execute()->fetchCol();
+    $this->assertEqual($results[0], 28, t('Query properly executed after unserialization.'));
+  }
+}
+
+/**
  * Range query tests.
  */
 class DatabaseRangeQueryTestCase extends DrupalWebTestCase {
