Added doctrine middleware

Change-Id: Ia0b8c7d141fee2f7f3e0598ca5293bdcfe488ecb
Signed-off-by: smarcet <smarcet@gmail.com>
This commit is contained in:
smarcet 2020-10-21 11:19:39 -03:00
parent bed3d61680
commit 89838d398e
3 changed files with 52 additions and 0 deletions

View File

@ -22,6 +22,7 @@ class Kernel extends HttpKernel
\Spatie\Cors\Cors::class,
\App\Http\Middleware\SecurityHTTPHeadersWriterMiddleware::class,
\App\Http\Middleware\ParseMultipartFormDataInputForNonPostRequests::class,
\App\Http\Middleware\DoctrineMiddleware::class,
];
/**

View File

@ -0,0 +1,49 @@
<?php namespace App\Http\Middleware;
/**
* Copyright 2020 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Closure;
use Illuminate\Support\Facades\Log;
use LaravelDoctrine\ORM\Facades\Registry;
use models\utils\SilverstripeBaseModel;
/**
* Class DoctrineMiddleware
* @package App\Http\Middleware
*/
class DoctrineMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
// before response close it
try {
$em = Registry::getManager(SilverstripeBaseModel::EntityManager);
if ($em) {
$em->getConnection()->close();
$em->close();
}
}
catch (\Exception $ex){
Log::error($ex);
}
return $response;
}
}

View File

@ -93,6 +93,8 @@ final class DoctrineTransactionService implements ITransactionService
Log::warning($ex);
$retry++;
if ($retry === self::MaxRetries) {
$em->close();
$con->rollBack();
throw $ex;
}
} catch (Exception $ex) {