Skip to content

Commit 5191d63

Browse files
xurshudyanXurshudyan
andauthored
Ensure database connection is always restored in usingConnection() (#56258)
Co-authored-by: Xurshudyan <[email protected]>
1 parent fac2d77 commit 5191d63

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/Illuminate/Database/DatabaseManager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,11 @@ public function usingConnection($name, callable $callback)
354354

355355
$this->setDefaultConnection($name);
356356

357-
return tap($callback(), function () use ($previousName) {
357+
try {
358+
return $callback();
359+
} finally {
358360
$this->setDefaultConnection($previousName);
359-
});
361+
}
360362
}
361363

362364
/**

src/Illuminate/Database/Migrations/Migrator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,11 @@ public function usingConnection($name, callable $callback)
662662

663663
$this->setConnection($name);
664664

665-
return tap($callback(), fn () => $this->setConnection($previousConnection));
665+
try {
666+
return $callback();
667+
} finally {
668+
$this->setConnection($previousConnection);
669+
}
666670
}
667671

668672
/**

0 commit comments

Comments
 (0)