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 #2463321: Serializing the database connection is dangerous and error-prone, make it unserializable again, 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.
- #2893917: Create a version of StringTranslationTrait that uses DependencySerializationTrait
- 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 #2893029: EntityType objects cannot be reliably serialized without DependencySerializationTrait

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.'

Comments

johnv created an issue. See original summary.

johnv’s picture

Issue summary: View changes
johnv’s picture

Issue summary: View changes
johnv’s picture

johnv’s picture

johnv’s picture

johnv’s picture

Title: LogicException: The database connection is not serializable. [META] » LogicException: The database connection is not serializable (for string translations in Ajax callback) [META]
johnv’s picture

Issue summary: View changes
johnv’s picture

Issue summary: View changes
quietone’s picture

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

@johnv, thanks for organizing the work on this problem!

Moving "[meta]" to the begining of the title per Special issue titles.

alexpott’s picture

@johnv I have tried the steps to reproduce and I can not make it happen. I think this is an important bug to fix but we need to understand the root cause. Ie what is causing the LocaleLookup to be serialized. Just you make your instructions to reproduce even clearer. Here are some questions I have:

  • Are you starting from Standard?
  • Do you have other contrib installed?
  • What is the contrib field module you are using? I tried Address and it worked perfectly.
johnv’s picture

@alexpott, please see my response in #2987548-43: LogicException: The database connection is not serializable. with a test case using only the office_hours contrib module, hitting an AJAX button.