Ensured EntityManager is reopened by CloseDbConnectionMiddleware after an error closed it

This commit is contained in:
Alejandro Celaya
2019-07-31 20:54:41 +02:00
parent 406de16a0d
commit f5878a5e7b
3 changed files with 44 additions and 0 deletions

View File

@@ -3,11 +3,13 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Middleware;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
class CloseDbConnectionMiddleware implements MiddlewareInterface
{
@@ -23,6 +25,16 @@ class CloseDbConnectionMiddleware implements MiddlewareInterface
{
try {
return $handler->handle($request);
} catch (Throwable $e) {
// FIXME Mega ugly hack to avoid a closed EntityManager to make shlink fail forever on swoole contexts
// Should be fixed with request-shared EntityManagers, which is not supported by the ServiceManager
if (! $this->em->isOpen()) {
(function () {
$this->closed = false;
})->bindTo($this->em, EntityManager::class)();
}
throw $e;
} finally {
$this->em->getConnection()->close();
$this->em->clear();