DatabaseExceptionWrapper: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: INSERT INTO "cache_default"

Created on 9 May 2024, about 2 months ago
Updated 10 May 2024, about 2 months ago

Problem/Motivation

hi, i have created a custom form, during the form building, api calls are made inside a while loop until the api returns a specific status then the loop breaks, some time the form works fine sometime the form freezes especially when the while loop runs for long and this error is in the log:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: INSERT INTO "cache_default" ("cid", "expire", "created", "tags", "checksum", "data", "serialized") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6) ON DUPLICATE KEY UPDATE "cid" = VALUES("cid"), "expire" = VALUES("expire"), "created" = VALUES("created"), "tags" = VALUES("tags"), "checksum" = VALUES("checksum"), "data" = VALUES("data"), "serialized" = VALUES("serialized"); Array ( [:db_insert_placeholder_0] => honeypot_protected_forms [:db_insert_placeholder_1] => -1 [:db_insert_placeholder_2] => 1715191489.933 [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => a:1:{i:0;s:18:"user_register_form";} [:db_insert_placeholder_6] => 1 ) in Drupal\Core\Cache\DatabaseBackend->doSetMultiple() (line 283 of /home/944222.cloudwaysapps.com/mgqgsustmd/public_html/web/core/lib/Drupal/Core/Cache/DatabaseBackend.php).

i was able to resolve this issue by putting this line of code inside the while loop:

\Drupal::service('cache.default')->delete('honeypot_protected_forms');

as you see it deletes the honeypot_protected_forms from the cache_default table, so we donot get the duplicate error, now you should know that i have not applied honeypot to this custom form, i think the reason why the honeypot is called for the form even though its not being used for this form is because on line 97 in honeypot.module file is this code:

elseif (in_array($form_id, \Drupal::service('honeypot')->getProtectedForms())) {

the getProtectedForms() function in the service contains this code:

$this->cacheBackend->set('honeypot_protected_forms', $forms);

so the cache is saved for every form even if those forms donot have the honeypot applied, so please first tell me if i am doing the right thing by deleting the honeypot_protected_forms from the cache_default table in my custom form while loop and if the code in line 97 in honeypot.module file must be changed so that the honeypot_protected_forms is not saved in cache for those forms for which the honeypot is not applied.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Active

Version

2.1

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dastan56
  • πŸ‡ΊπŸ‡ΈUnited States TR Cascadia

    "MySQL server has gone away" in Drupal usually means your database configuration settings need to be changed to handle the number and size of the database requests made while loading a Drupal page. If you search this site for that error, you will find many discussions for this and documentation of what settings to change.

    "so the cache is saved for every form even if those forms donot have the honeypot applied"

    No, that's not true. This code is in the service, and the cache data is lazy-loaded into memory once per page load, and all subsequent calls to getProtectedForms() on that page use the copy in memory. Read the API documentation on the use of drupal_static() to see how this works. The database cache is just used to persist the list of protected forms across page loads.

    But if you delete the cache entry, then the list of protected forms needs to be rebuilt each time

  • Thank you for your response, but there is one thing you did not address, which is that the error contains this:

    ON DUPLICATE KEY UPDATE "cid" =

    as you see the error happens, because when it tries to add honeypot_protected_forms to the cache_default table, it gets error that the key already exists, and this is why i delete it so that this duplication error donot happen, thanks again.

  • πŸ‡ΊπŸ‡ΈUnited States TR Cascadia

    No, the error does not say a duplicate key exists. "ON DUPLICATE KEY UPDATE" is part of the SQL command that tells the database what to do in the case of a duplicate key. Specifically, it tells the database to UPDATE instead of INSERT if there is a duplicate key.

    The error is "General error: 2006 MySQL server has gone away". The fact that it happens when trying to update a honeypot table is just coincidental - the number or size of your database queries has exceeded the capacity of your database as configured, and you need to change your database configuration settings to accommodate this.

  • ok, thanks again, as i said for now using this code has resolved this issue for me:

    \Drupal::service('cache.default')->delete('honeypot_protected_forms');

    i will keep an eye on it and see if the form freezes again.

Production build 0.69.0 2024