From 4ef2ff9bae192afa1bc40407a5a036455d5b501c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Oct 2024 13:03:28 +1000 Subject: [PATCH] Improve try/catch with transaction rollbacks --- app/Jobs/Bank/MatchBankTransactions.php | 4 ---- app/Libraries/MultiDB.php | 1 + .../Contact/UpdateContactLastLogin.php | 3 +-- app/Services/Client/ClientService.php | 23 +++---------------- 4 files changed, 5 insertions(+), 26 deletions(-) diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index 402edbebd4..c2ff03eee6 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -294,8 +294,6 @@ class MatchBankTransactions implements ShouldQueue $this->attachable_invoices = []; $this->available_balance = $amount; - nlog($invoices->count()); - \DB::connection(config('database.default'))->transaction(function () use ($invoices) { $invoices->each(function ($invoice) { $this->invoice = Invoice::withTrashed()->where('id', $invoice->id)->lockForUpdate()->first(); @@ -329,8 +327,6 @@ class MatchBankTransactions implements ShouldQueue }); }, 2); - nlog("pre"); - // @phpstan-ignore-next-line if (!$this->invoice) { return; diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index cc3545b927..a361d3b584 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -46,6 +46,7 @@ class MultiDB public const DB_PREFIX = 'db-ninja-'; public static $dbs = ['db-ninja-01', 'db-ninja-02']; + // public static $dbs = ['db-ninja-01', 'db-ninja-02', 'db-ninja-03']; private static $protected_domains = [ 'www', diff --git a/app/Listeners/Contact/UpdateContactLastLogin.php b/app/Listeners/Contact/UpdateContactLastLogin.php index 3cdd98e59f..75c416bc6c 100644 --- a/app/Listeners/Contact/UpdateContactLastLogin.php +++ b/app/Listeners/Contact/UpdateContactLastLogin.php @@ -16,7 +16,6 @@ use Illuminate\Contracts\Queue\ShouldQueue; class UpdateContactLastLogin implements ShouldQueue { - public $delay = 8; /** * Create the event listener. * @@ -37,8 +36,8 @@ class UpdateContactLastLogin implements ShouldQueue MultiDB::setDb($event->company->db); $client_contact = $event->client_contact; - $client_contact->last_login = now(); $client_contact->save(); + } } diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index e966f6f7a4..1e8e14427d 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -89,12 +89,7 @@ class ClientService DB::connection(config('database.default'))->rollBack(); } - } catch(\Exception $exception) { - - if (DB::connection(config('database.default'))->transactionLevel() > 0) { - DB::connection(config('database.default'))->rollBack(); - } - } + } return $this; } @@ -115,13 +110,7 @@ class ClientService DB::connection(config('database.default'))->rollBack(); } - } catch(\Exception $exception) { - nlog("DB ERROR " . $exception->getMessage()); - - if (DB::connection(config('database.default'))->transactionLevel() > 0) { - DB::connection(config('database.default'))->rollBack(); - } - } + } return $this; } @@ -141,13 +130,7 @@ class ClientService DB::connection(config('database.default'))->rollBack(); } - } catch(\Exception $exception) { - nlog("DB ERROR " . $exception->getMessage()); - - if (DB::connection(config('database.default'))->transactionLevel() > 0) { - DB::connection(config('database.default'))->rollBack(); - } - } + } return $this; }