diff -u b/core/lib/Drupal/Core/Entity/entity.api.php b/core/lib/Drupal/Core/Entity/entity.api.php --- b/core/lib/Drupal/Core/Entity/entity.api.php +++ b/core/lib/Drupal/Core/Entity/entity.api.php @@ -1233,6 +1233,7 @@ * @see hook_ENTITY_TYPE_predelete() */ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) { + $connection = \Drupal::database(); // Count references to this entity in a custom table before they are removed // upon entity deletion. $id = $entity->id(); @@ -1261,6 +1262,7 @@ * @see hook_entity_predelete() */ function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity) { + $connection = \Drupal::database(); // Count references to this entity in a custom table before they are removed // upon entity deletion. $id = $entity->id(); @@ -1273,7 +1275,7 @@ ->fetchField(); // Log the count in a table that records this statistic for deleted entities. - \Drupal::database()->merge('example_deleted_entity_statistics') + $connection->merge('example_deleted_entity_statistics') ->key(['type' => $type, 'id' => $id]) ->fields(['count' => $count]) ->execute(); diff -u b/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module --- b/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -250,6 +250,7 @@ * The node updated timestamp or comment timestamp. */ function _tracker_add($nid, $uid, $changed) { + $connection = \Drupal::database(); // @todo This should be actually filtering on the desired language and just // fall back to the default language. $node = db_query('SELECT nid, status, uid, changed FROM {node_field_data} WHERE nid = :nid AND default_langcode = 1 ORDER BY changed DESC, status DESC', [':nid' => $nid])->fetchObject(); @@ -259,7 +260,6 @@ $changed = max($node->changed, $changed); // Update the node-level data. - $connection = \Drupal::database(); $connection->merge('tracker_node') ->key('nid', $nid) ->fields([ diff -u b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php --- b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php +++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php @@ -33,7 +33,8 @@ */ public function testGarbageCollection() { $collection = $this->randomMachineName(); - $store = new DatabaseStorageExpirable($collection, new PhpSerialize(), Database::getConnection()); + $connection = Database::getConnection(); + $store = new DatabaseStorageExpirable($collection, new PhpSerialize(), $connection); // Insert some items and confirm that they're set. for ($i = 0; $i <= 3; $i++) { @@ -41,7 +42,6 @@ } $this->assertIdentical(count($store->getAll()), 4, 'Four items were written to the storage.'); - $connection = \Drupal::database(); // Manually expire the data. for ($i = 0; $i <= 3; $i++) { $connection->merge('key_value_expire')