diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
index 7dad008..939ed0c 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
@@ -14,6 +14,7 @@
 use Drupal\Core\Database\TransactionCommitFailedException;
 use Drupal\Core\Database\DatabaseException;
 use Drupal\Core\Database\Connection as DatabaseConnection;
+use Drupal\Component\Utility\Unicode;
 
 /**
  * @addtogroup database
@@ -35,6 +36,16 @@ class Connection extends DatabaseConnection {
   protected $needsCleanup = FALSE;
 
   /**
+   * The minimal possible value for the max_allowed_packet setting of MySQL.
+   *
+   * @link https://mariadb.com/kb/en/mariadb/server-system-variables/#max_allowed_packet
+   * @link https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_allowed_packet
+   *
+   * @var int
+   */
+  const MIN_MAX_ALLOWED_PACKET = 1024;
+
+  /**
    * Constructs a Connection object.
    */
   public function __construct(\PDO $connection, array $connection_options = array()) {
@@ -52,6 +63,24 @@ public function __construct(\PDO $connection, array $connection_options = array(
   /**
    * {@inheritdoc}
    */
+  public function query($query, array $args = array(), $options = array()) {
+    try {
+      return parent::query($query, $args, $options);
+    } catch (DatabaseException $e) {
+      if ($e->getPrevious()->errorInfo[1] == 1153) {
+        // If a max_allowed_packet error occurs the message length is truncated.
+        // This should prevent the error from reoccuring if the exception is
+        // logged to the database using dblog or the like.
+        $message = Unicode::truncateBytes($e->getMessage(), $this->getMaxAllowedPacket());
+        $e = new DatabaseExceptionWrapper($message, 0, $e->getPrevious());
+      }
+      throw $e;
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public static function open(array &$connection_options = array()) {
     // The DSN should use either a socket or a host/port.
     if (isset($connection_options['unix_socket'])) {
@@ -278,6 +307,24 @@ protected function popCommittableTransactions() {
       }
     }
   }
+
+  /**
+   * Get the current value for the max_allowed_packet setting of MySQL.
+   *
+   * @return int
+   *  The max_allowed_packet setting of the MySQL server.
+   */
+  public function getMaxAllowedPacket() {
+    try {
+      // @todo This query has to use the dedicated watchdog connection.
+      //    See https://www.drupal.org/node/298768
+      return db_query('SELECT @@global.max_allowed_packet')->fetchField();
+    }
+    catch (Exception $e) {
+      // If any exception occurs just return the safe minimal value.
+      return Connection::MIN_MAX_ALLOWED_PACKET;
+    }
+  }
 }
 
 
diff --git a/core/modules/system/src/Tests/Database/LargeQueryTest.php b/core/modules/system/src/Tests/Database/LargeQueryTest.php
new file mode 100644
index 0000000..f28bd13
--- /dev/null
+++ b/core/modules/system/src/Tests/Database/LargeQueryTest.php
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\system\Tests\Database\LargeQueryTest.
+ */
+
+namespace Drupal\system\Tests\Database;
+
+use Drupal\Core\Database\Database;
+use Drupal\Core\Database\DatabaseException;
+
+/**
+ * Tests handling of large queries.
+ *
+ * @group Database
+ */
+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.
+      $max_allowed_packet = Database::getConnection()->getMaxAllowedPacket();
+      if (\Drupal\Component\Utility\Environment::checkMemoryLimit($max_allowed_packet + (16 * 1024 * 1024))) {
+        $long_name = str_repeat('a', $max_allowed_packet + 1);
+        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($e->getPrevious()->errorInfo[1], 1153, "Got a packet bigger than 'max_allowed_packet' bytes exception thrown.");
+          // Use strlen() to count the bytes exactly, not the unicode chars.
+          $this->assertEqual(strlen($e->getMessage()), $max_allowed_packet, "'max_allowed_packet' exception message truncated.");
+        }
+      }
+      else {
+        $this->verbose('The configured max_allowed_package exceeds the php memory limit. Therefore the test is skipped.');
+      }
+    }
+    else {
+      $this->verbose('The test requires MySQL. Therefore the test is skipped.');
+    }
+  }
+
+}
