diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/LargeQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/LargeQueryTest.php
new file mode 100644
index 0000000..99facbe
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Database/LargeQueryTest.php
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\system\Tests\Database\LargeQueryTest.
+ */
+
+namespace Drupal\system\Tests\Database;
+
+use Drupal\Core\Database\Database;
+use Drupal\Core\Database\DatabaseException;
+use Drupal\Core\Database\Connection;
+
+/**
+ * Tests handling of large queries.
+ */
+class LargeQueryTest extends DatabaseTestBase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Large query handling',
+      'description' => 'Test handling of large queries.',
+      'group' => 'Database',
+    );
+  }
+
+  /**
+   * Tests that we truncate queries in exception messages when a
+   * max_allowed_packet error occurs. This should avoid subsequent errors
+   * if the exeception is then logged to the database.
+   */
+  function testMaxAllowedPacketQueryTruncating() {
+    // This test only makes sense if we are running on a MySQL database.
+    // Test if we are.
+    $database = Database::getConnectionInfo('default');
+    if ($database['default']['driver'] == 'mysql') {
+      // The max_allowed_packet value is configured per database instance.
+      // Retrieve the max_allowed_packet value from the current instance and
+      // check if PHP is configured with sufficient allowed memory to be able
+      // to generate a query larger than max_allowed_packet.
+      $result = db_query("SHOW VARIABLES WHERE Variable_name = 'max_allowed_packet'")->fetchCol(1);
+      $max_allowed_packet = $result[0];
+      if (drupal_check_memory_limit($max_allowed_packet)) {
+        $long_name = str_repeat('a', $max_allowed_packet);
+        try {
+          db_query('SELECT name FROM {test} WHERE name = :name', array(':name' => $long_name));
+          $this->fail("An exception should be thrown for queries larger than 'max_allowed_packet'");
+        } catch (DatabaseException $e) {
+          $this->assertEqual(strlen($e->getMessage()), \Drupal\Core\Database\Driver\mysql\Connection::MAX_ALLOWED_PACKET_MESSAGE_LENGTH, "'max_allowed_packet' exception message truncated");
+        }
+      }
+    }
+  }
+
+}
