gpt4 book ai didi

symfony - 在 DBAL 和 session 处理程序 Symfony2 之间共享数据库连接

转载 作者:行者123 更新时间:2023-12-01 00:44:25 25 4
gpt4 key购买 nike

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

配置文件

framework:
session:
handler_id: session.handler.one_connection_pdo

服务.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);
}

}

浏览应用程序时似乎一切正常,但我无法更新任何实体,因为出现错误:
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

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

//编辑

终于在 PdoSessionHandler里面找到了解决办法类(class)。

默认情况下,PDO 处理程序在读取和写入 session 时使用事务。它在第一个 read() 开始交易并提交 close() .中间有一些数据库操作 $em->persist($entity); $em->flush()这产生了另一个产生错误的事务。

PdoSessionHandler class 我发现有一个 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 处理程序将使用咨询锁而不是事务,并且不会再有事务错误。

最佳答案

在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 }

关于symfony - 在 DBAL 和 session 处理程序 Symfony2 之间共享数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35558177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com