如何使用Symfony2控制台命令中的Master / Slave doctrine2配置 [英] How to use Master/Slave doctrine2 configurations from Symfony2 Console commands

查看:200
本文介绍了如何使用Symfony2控制台命令中的Master / Slave doctrine2配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Symfony2中使用Doctrine2主/从配置来扩展我的应用程序跨单个主数据库和几个只读(复制)从站。

I'm using the Doctrine2 master/slave configuration in Symfony2 to scale my application across a single master database and several read-only (replicated) slaves.

这在我的应用程序中完美地工作,Doctrine2明智地使用从属设备进行只读查询和主机进行写操作。

This works perfectly from within my application and Doctrine2 sensibly uses the slave for read-only queries and the master for write operations.

如果在我的一个控制器中,我写道:

If in one of my controllers, I wrote:

$em = $this->get('doctrine')->getManager();
var_dump($em->getConnection()->isConnectedToMaster());

isConnectedToMaster()返回 false - 我希望从默认情况下,从机被连接到。

isConnectedToMaster() returns false - which I would expect since the slave is connected to by default.

如果我在 WebTestCase

但是,如果我在Symfony控制台命令(ContainerAwareCommand)中编写完全相同的代码:

However if I wrote the exact same code inside a Symfony console command (ContainerAwareCommand):

$em = $this->getContainer()->get('doctrine')->getManager();
var_dump($em->getConnection()->isConnectedToMaster());

isConnectedToMaster()返回true。这意味着主人被选为默认值。

isConnectedToMaster() returns true. Which means the master is selected as the default.

我无法找到如何通过控制台命令停止作为主服务器的默认连接。这意味着如果我想从控制台运行一些非关键的,繁重的数据处理任务 - 他们都会击中主(坏)而不是一个奴隶(不错)。

I can't find out how to stop the default connection being master from console commands. Which means if I want to run some non-critical, heavy data processing tasks from the console - they all hit the master (bad) not one of the slaves (good).

任何人都知道如何让Doctrine2默认使用从机从控制台?或者知道为什么它总是默认为主人?

Anyone know how I can make Doctrine2 use the slave by default from the console? Or know why it always defaults to the master?

解答:感谢nifr谁让我在正确的课程 - 我发现我的问题是因为我使用JMS \\JobQueueBundle(它将app / console使用的应用程序替换为必须以强制选择主的方式连接到MasterSlaveConnection的更改应用程序)。当我注释出

ANSWERED: Thanks to nifr who set me on the right course - I found that my problem was because I was using JMS\JobQueueBundle (which replaces the Application used by app/console with an altered one which must connect to the MasterSlaveConnection in a way which forces the "master" to be chosen). When I commented out the

use JMS\JobQueueBundle\Console\Application;

app / console 中,在我的控制台测试中正确选择。

in app/console, the slave was correctly selected in my console test.

谢谢

推荐答案

以确保您的命令不会调用允许教条选择主连接的任何操作。

You have to make sure your command doesn't call any action that lets doctrine choose the master connection.

MasterSlaveConnection 应该是如何以及何时选择奴隶或主人。

Important for the understanding of the MasterSlaveConnection should be how and when it picks the slave or master.


  • 选择如果使用getWrappedConnection或executeQuery,则从未选择主从属

  • exec ',' executeUpdate ','插入','删除','更新 createSavepoint ',' releaseSavepoint ',' beginTransaction ',' rollback 查询准备

  • 如果在连接生命周期中选择了主人,它将始终被选中。

  • 一个从属连接随机选择ONCE一个请求。

  • picks Slave if Master was never picked before and ONLY if 'getWrappedConnection' or 'executeQuery' is used.
  • Master picked when 'exec', 'executeUpdate', 'insert', 'delete', 'update', 'createSavepoint', 'releaseSavepoint', 'beginTransaction', 'rollback', 'commit', 'query' or 'prepare' is called.
  • If master was picked once during the lifetime of the connection it will always get picked afterwards.
  • One slave connection is randomly picked ONCE during a request.

MasterSlaveConnection API

这篇关于如何使用Symfony2控制台命令中的Master / Slave doctrine2配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆