I am trying to use the same connection with my database between the session handler and the dbal doctrine:
config.yml
framework:
session:
handler_id: session.handler.one_connection_pdo
services.yml
session.handler.one_connection_pdo:
class: AppBundle\Session\OneConnectionPdoHandler
public: false
arguments:
- "@database_connection"
- []
AppBundle / Session / OneConnectionPdoHandler.php
namespace AppBundle\Session;
use Doctrine\DBAL\Connection;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
class OneConnectionPdoHandler extends PdoSessionHandler
{
public function __construct($pdoOrDsn, array $options)
{
if ($pdoOrDsn instanceof Connection) {
$pdoOrDsn = $pdoOrDsn->getWrappedConnection();
}
parent::__construct($pdoOrDsn, $options);
}
}
Everything seems to work when viewing the application, but I cannot update any object because I get an error:
PDOException: There is already an active transaction
at n/a
in .../vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php line 1176
at PDO->beginTransaction()
in .../vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php line 1176
at Doctrine\DBAL\Connection->beginTransaction()
in .../vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 373
at Doctrine\ORM\UnitOfWork->commit(null)
in .../vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php line 356
at Doctrine\ORM\EntityManager->flush()
in .../src/AppBundle/Controller/Admin/DistributorsController.php line 66
at AppBundle\Controller\Admin\DistributorsController->editAction(object(Distributor), object(Request))
in line
at call_user_func_array(array(object(DistributorsController), 'editAction'), array(object(Distributor), object(Request)))
in .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 139
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
in .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 62
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
in .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
in .../web/app_dev.php line 31
Is there a way to share the relationship between the dbal doctrine and the pdo custom handler?
// EDIT
Finally, I found a solution inside the class PdoSessionHandler.
By default, the PDO handler uses a transaction when reading and writing to the session. It starts the transaction again read()and commits to close(). Between them there was some database operation $em->persist($entity); $em->flush()that generated another transaction that generated an error.
PdoSessionHandler , lock_mode, :
session.handler.one_connection_pdo:
class: AppBundle\Session\OneConnectionPdoHandler
public: false
arguments:
- "@database_connection"
- { lock_mode: 1 }
lock_mode 1 (PdoSessionHandler::LOCK_ADVISORY), PDO , .