From bd348bb577a8ecc48a770d12265d130f98a8747d Mon Sep 17 00:00:00 2001
From: Benn Oshrin <boshrin@users.noreply.github.com>
Date: Tue, 27 Dec 2022 12:25:19 -0500
Subject: [PATCH] Additional fix for CO-2235 (CO-2564)

---
 app/src/Command/BulkLoadCommand.php               |  4 ++--
 app/src/Controller/MatchgridRecordsController.php | 10 ++++++++--
 app/src/Controller/MatchgridsController.php       | 10 ++++++++--
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/app/src/Command/BulkLoadCommand.php b/app/src/Command/BulkLoadCommand.php
index b7bbcb7b8..10061ca85 100644
--- a/app/src/Command/BulkLoadCommand.php
+++ b/app/src/Command/BulkLoadCommand.php
@@ -126,7 +126,7 @@ public function execute(Arguments $args, ConsoleIo $io) {
     try {
       $MatchService = new \App\Lib\Match\MatchService();
       
-      $MatchService->connect();
+      $MatchService->connectDatabase();
       $MatchService->setConfig($mg->id);
       
       // First see if table is already built. If not, throw an error that the
@@ -261,7 +261,7 @@ public function execute(Arguments $args, ConsoleIo $io) {
         $MatchgridBuilder->build($mg, $mg->attributes, true);
       }
       
-      $MatchService->disconnect();
+      $MatchService->disconnectDatabase();
     }
     catch(\Exception $e) {
       $io->out($e->getMessage());
diff --git a/app/src/Controller/MatchgridRecordsController.php b/app/src/Controller/MatchgridRecordsController.php
index cd0c345fd..478e4ed2c 100644
--- a/app/src/Controller/MatchgridRecordsController.php
+++ b/app/src/Controller/MatchgridRecordsController.php
@@ -107,7 +107,7 @@ public function add() {
         
         $MatchService = new \App\Lib\Match\MatchService();
         
-        $MatchService->connect();
+        $MatchService->connectDatabase();
         $MatchService->setConfig($this->cur_mg->id);
         
         // Before we perform the request, see if we already have an entry for this SORID
@@ -231,7 +231,7 @@ public function edit($id) {
         
         $MatchService = new MatchService();
         
-        $MatchService->connect();
+        $MatchService->connectDatabase();
         $MatchService->setConfig($this->cur_mg->id);
         
         // Unlike add (which verifies no entries for sor+sorid), we don't sanity
@@ -436,6 +436,8 @@ protected function performMatch(MatchService $MatchService, string $sor, string
     // Did any rules run successfully? If not (eg: no attributes provided in the
     // request, no rules defined) then throw an error.
     if(empty($results->getSuccessfulRules())) {
+      $MatchService->disconnectDatabase();
+      
       throw new \RuntimeException(__('match.er.rules.unsuccessful'));
     }
     
@@ -457,6 +459,8 @@ protected function performMatch(MatchService $MatchService, string $sor, string
       // Fuzzy match, we insert the record but do NOT send notification
       $matchRequest = $MatchService->insertPending($sor, $sorid, $AttributeManager);
       
+      $MatchService->disconnectDatabase();
+
       $this->Flash->information(__('match.rs.refid.pending', [$matchRequest]));
       
       // Redirect to the reconcilation page
@@ -469,6 +473,8 @@ protected function performMatch(MatchService $MatchService, string $sor, string
     }
     
     $id = $MatchService->getRequestIdForSorId($sor, $sorid);
+
+    $MatchService->disconnectDatabase();
     
     return $this->redirect([
       'action' => 'edit',
diff --git a/app/src/Controller/MatchgridsController.php b/app/src/Controller/MatchgridsController.php
index 97112f42b..e1000cf58 100644
--- a/app/src/Controller/MatchgridsController.php
+++ b/app/src/Controller/MatchgridsController.php
@@ -149,11 +149,13 @@ public function pending(string $id) {
     try {
       $MatchService = new \App\Lib\Match\MatchService();
       
-      $MatchService->connect();
+      $MatchService->connectDatabase();
       $MatchService->setConfig((int)$id);
       
       $results = $MatchService->getRequests('pending');
       
+      $MatchService->disconnectDatabase();
+
       // Although we're passing the $id as provided by the user, it has been
       // vetted since MatchgridLinkTrait will pull the current Matchgrid from
       // the database before we get here and throw an error if $id is invalid.
@@ -182,7 +184,7 @@ public function reconcile(string $id) {
       $MatchService = new \App\Lib\Match\MatchService();
       $AttributeManager = new \App\Lib\Match\AttributeManager();
       
-      $MatchService->connect();
+      $MatchService->connectDatabase();
       $MatchService->setConfig((int)$id);
       
       if($this->request->is('post')) {
@@ -285,6 +287,8 @@ public function reconcile(string $id) {
           }
         }
         
+        $MatchService->disconnectDatabase();
+
         // Redirect back to list of pending requests
         return $this->redirect([
           'action' => 'pending',
@@ -341,6 +345,8 @@ public function reconcile(string $id) {
         // Also set the original request separately to make it easier for the view
         $this->set('vv_request', $origReq[$rowId]);
         $this->set('vv_title', __('match.op.reconcile.request', [$sor, $sorid]));
+        
+        $MatchService->disconnectDatabase();
       } // is post
     }
     catch(Exception $e) {