Transactions 如何在学说中使用不同的联系?

Transactions 如何在学说中使用不同的联系?,transactions,doctrine,connection,Transactions,Doctrine,Connection,我尝试在条令(v1.2.2)中使用几个事务。 以下是我的测试: // Open a new database connection $cnx1 = Doctrine_Manager::connection('mydsn'); // $cnx1 will be named '0' // Open a second database connection $cnx2 = Doctrine_Manager::connection('mydsn'); // $cnx2 will be named '1

我尝试在条令(v1.2.2)中使用几个事务。 以下是我的测试:

// Open a new database connection
$cnx1 = Doctrine_Manager::connection('mydsn'); // $cnx1 will be named '0'

// Open a second database connection
$cnx2 = Doctrine_Manager::connection('mydsn'); // $cnx2 will be named '1'

// Start a transaction on connection #1
$cnx1->beginTransaction();
// Update the name of user #1
$query1 = $cnx1->createQuery();
$query1->update('SfGuardUser')->set("username", "'Name 1'")->where("id='1'");
$query1->execute();

// Start an other transaction on connection #2
$cnx2->beginTransaction();
// Update the name of user #2
$query2 = $cnx2->createQuery();
$query2->update('SfGuardUser')->set("username", "'Name 2'")->where("id='2'");
$query2->execute();

// Commit transaction #2
$cnx2->commit();
(默认隔离级别为可重复读取。)

运行代码后,数据库中的两个用户名都已更改。 根据我的说法,只应该修改用户2的名称,因为事务1尚未提交

调试时,我看到所有查询都使用连接2(最后打开的连接)。为什么?

在条令文档中,我们可以读到“从一开始条令就被设计用于处理多个连接。除非单独指定,条令总是使用当前连接来执行查询。”


如何使用不同的连接和uniques事务?

我认为这是原则上的一个缺陷(据我所知,目前的1.2版本仍然存在)。从Doctrine_Connection::createQuery()创建查询时,它不会将自身作为第一个参数传递给查询,因此它始终在默认连接上创建

错误修复应该是一个简单的错误修复,将$this传递给条令\u连接中的条令\u查询::create()

解决方法是自己创建查询,并传入连接:

$query1 = Doctrine_Query::create($cnx1);
$query2 = Doctrine_Query::create($cnx2);

米沙尔第1条教义的固定和分叉如下: