From 2da4e3d2b3b094b92b277a3365beadeab81c0974 Mon Sep 17 00:00:00 2001
From: Morton Jonuschat <m.jonuschat@mojocode.de>
Date: Mon, 5 Jun 2017 20:55:38 -0700
Subject: [PATCH] [BUGFIX] Use correct database connection for table in
 ReferenceIndex

Reusing the connection object for the sys_refindex table can fail
if the table that is being indexed is actually on another connection.
Select the proper connection before attempting to execute the query.

Change-Id: I4c031428f7405031aea6ef652c8b6f488f921228
Resolves: #81049
Releases: master, 8.7
Reviewed-on: https://review.typo3.org/53131
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Henrik Elsner <helsner@dfau.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 typo3/sysext/core/Classes/Database/ReferenceIndex.php | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/core/Classes/Database/ReferenceIndex.php b/typo3/sysext/core/Classes/Database/ReferenceIndex.php
index 4598632c2075..ea1fba78999f 100644
--- a/typo3/sysext/core/Classes/Database/ReferenceIndex.php
+++ b/typo3/sysext/core/Classes/Database/ReferenceIndex.php
@@ -207,7 +207,8 @@ class ReferenceIndex
             $tableRelationFields = $this->runtimeCache->get($cacheId);
         }
 
-        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_refindex');
+        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
+        $connection = $connectionPool->getConnectionForTable('sys_refindex');
 
         // Get current index from Database with hash as index using $uidIndexField
         // no restrictions are needed, since sys_refindex is not a TCA table
@@ -227,7 +228,7 @@ class ReferenceIndex
         }
 
         // If the table has fields which could contain relations and the record does exist (including deleted-flagged)
-        $queryBuilder = $connection->createQueryBuilder();
+        $queryBuilder = $connectionPool->getQueryBuilderForTable($tableName);
         $queryBuilder->getRestrictions()->removeAll();
 
         $exists = $queryBuilder
-- 
GitLab