. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Closure; use Illuminate\Database\Capsule\Manager as DB; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Throwable; /** * Middleware to wrap a request in a transaction. */ class UseTransaction implements MiddlewareInterface { /** * @param Request $request * @param Closure $next * * @return Response * @throws Throwable */ public function handle(Request $request, Closure $next): Response { try { DB::connection()->beginTransaction(); $response = $next($request); DB::connection()->commit(); return $response; } catch (Throwable $exception) { DB::connection()->rollBack(); throw $exception; } } }