在DBAL和会话处理程序Symfony2之间共享与数据库的连接

时间:2016-02-22 16:00:09

标签: symfony session pdo doctrine-orm connection

我正在尝试在会话处理程序和doctrine dbal之间使用与数据库相同的连接:

config.yml

find_by_css('#dual_national').set(true)

services.yml

framework:
    session:
        handler_id:  session.handler.one_connection_pdo

的appbundle /会话/ OneConnectionPdoHandler.php

session.handler.one_connection_pdo:
    class:     AppBundle\Session\OneConnectionPdoHandler
    public:    false
    arguments:
        - "@database_connection"
        - []

浏览应用程序时似乎一切正常,但我无法更新任何实体,因为我收到错误:

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);
    }

}

有没有办法在doctrine dbal和自定义pdo处理程序之间共享连接?

//编辑

我终于在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 课程中找到了一个解决方案。

默认情况下,PDO处理程序在读取和写入会话时使用事务。它首先在PdoSessionHandler启动事务并在read()上提交。在两者之间存在一些数据库操作close(),它产生了另一个产生错误的事务。

$em->persist($entity); $em->flush()课程内,我发现有一个PdoSessionHandler选项,可以像这样设置:

lock_mode

当lock_mode设置为1(session.handler.one_connection_pdo: class: AppBundle\Session\OneConnectionPdoHandler public: false arguments: - "@database_connection" - { lock_mode: 1 } )时,PDO处理程序将使用建议锁而不是事务,并且不会再出现事务错误。

1 个答案:

答案 0 :(得分:4)

在Symfony 3中

在(app \ config \ config.yml)中:

framework:
    session:
     handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler

(app \ config \ services.yml):

 Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
        arguments:
            - !service { class: PDO, factory: 'database_connection:getWrappedConnection' }
            - {lock_mode: 1 }