[meta] LogicException: The database connection is not serializable (for string translations in Ajax callback)

Created on 9 November 2023, about 1 year ago
Updated 15 April 2024, 7 months ago

Problem/Motivation

Creating this issue to unite many other issues, that are reported by contrib modules, in order to get a precise description of the problem.

Several contrib modules report problem when hitting an AJAX button. The expected action is not performed, instead nothing happens, or the following message appears:

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep()

Apparently, the Ajax buttons makes the page being cached (serialized), before the action is executed and the (presumably) cached page is re-established with new data.
The root cause seems to be πŸ› Serializing the database connection is dangerous and error-prone, make it unserializable again Fixed , which is a fix for yet another problem.

All reports mention the 'Interface translation' to be installed. Whenever this module is disabled, no problem exists.
Or (in my case) if the standard English language is used, the problem doesn't occur either.
(A test might be good what happens if you install Drupal with only 1 non-English language)

This issue contains a list of related problems in the Drupal core issue queue.
It is harder to find contrib issues.

This implies that the problem is in StringTranslationTrait.
The proposed solutions for the affected contrib/core code are:
- use t() instead of $this->t() (returning new FormattableMarkup vs. new TranslatableMarkup);
- avoid the translation in the object causing the problem;
- add 'use DependencySerializationTrait;' to the the object causing the problem;
But of course, there must be 1 single solution in core.
(That means: when the error is cause by a translation being cached/serialized.)

Remember: the affected class can be ANY class on the form, like this example, where Honeypot breaks Webform: #3279421: Figure out a way forward to be compatible with Honeypot to 2.1.0, which caused a BC break β†’
Ofcourse, the same serialization problem can occur for other reasons. - Those are NOT part of this problem.

Steps to reproduce

Test prerequisites:
- enable the 'locale' module (User interface translation), which also requires Language module
- enable a contrib (field) module
- on /admin/config/regional/language/add, add another non-English language (having both English and your favorite one)
- (optional) on admin/config/regional/language/detection , enable both 'Url' and 'user' , having 'Url' first
- (optional) on admin/structure/block , add the 'Language switcher' for easy switch
- create a content type TYPE, add a contrib field

Test preparation:
- in any language, create a node of above type, save the form;
Test execution 1 - no error expected:
- in default (English) language, create a node of above type (page /node/8/edit), save;
- in default (English) language, edit the node of above type, press the Ajax [Add more] button;
- expected result: the expected action is performed

Test execution 2 - error encountered:
- change the language of the user (in user data or via
- in the alternative language, create a node of above type (page /node/8/edit), save;
- in the alternative language, edit the node of above type, press the widget's Ajax button ('Add more', 'Delete', or any other text);
- expected result: the expected action is performed
- actual result: the above-mentioned error message is logged/displayed.

Proposed resolution

Find a solution which not bothers contrib code.
- πŸ“Œ Create a version of StringTranslationTrait that uses DependencySerializationTrait Active
- or just add 'DependencySerializationTrait' to 'StringTranslationTrait'
- or avoid translations to be serialized --> #2987548-39: LogicException: The database connection is not serializable. β†’
- or remove databaseConnection from the StringTranslationTrait;
- quick-fix: a better message (see below), to directly hint to a proper fix (you still have to find the offending object/class)
- This issue tries to solve the problem in FormState #3055287: BatchStorage fails to serialize/deserialize input batch object with FormState β†’
- ...

Remaining tasks

- propose solutions (in above list)
- add related issues to this issue
- define if the 'forms system' is the correct core Component.
- Propose a better message that clearly states

LogicException: [...] Alternatively, add use DependencySerializationTrait; to the proper object as a temporary solution. [...]

- at least, fix in core, like πŸ› EntityType objects cannot be reliably serialized without DependencySerializationTrait Fixed

User interface changes

None to be expected.

API changes

None to be expected.

Data model changes

None to be expected.

Release notes snippet

pressumabley something like:
'When using use StringTranslationTrait; in contrib modules, the additionally added use DependencySerializationTrait; is now redundant/obsolete.'

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated 2 days ago

Created by

πŸ‡³πŸ‡±Netherlands johnv

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

Comments & Activities

Production build 0.71.5 2024