我正在尝试使用 Doctrine2 将 With(nolock) 提示附加到我的查询中。我尝试使用,->setLockMode(LockMode::NONE)->getSQL();
但它没有按预期工作,它将 WITH(NOLOCK) 添加到FROM 子句中的 ( first ) 表中。我会解释:
$dql='SELECT e, o FROM Porject:Example e JOIN e.owner o';
$query = $this->getEntityManager()->createQuery($dql);
try{
$this->getEntityManager()->getConnection()->beginTransaction();
$result = $query ->setLockMode(LockMode::NONE)->getSQL();
$this->getEntityManager()->getConnection()->commit();
} catch (\Exception $e) {
$this->getEntityManager()->getConnection()->rollback();
throw $e;
}
预期的:
SELECT c0_.prop1, c0_.prop2, c1_.prop1, c1_.prop2
FROM examples c0_ WITH(NOLOCK)
INNER JOIN owners c1_ WITH(NOLOCK) ON c1_.id= c0_ownerId`
我真正得到的:
SELECT c0_.prop1, c0_.prop2, c1_.prop1, c1_.prop2
FROM examples c0_ WITH(NOLOCK)
INNER JOIN owners c1_ ON c1_.id= c0_ownerId`
可能的解决方案:
try {
$this->getEntityManager()->getConnection()->beginTransaction();
$this->getEntityManager()->getConnection()->setTransactionIsolation(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED);
$result = $query->getArrayResult();
$this->getEntityManager()->getConnection()->commit();
} catch (\Exception $e) {
$this->getEntityManager()->getConnection()->rollback();
throw $e;
}
但我不确定这样做是否with(nolock)
与使用READ_UNCOMMITTED
隔离级别相同。
编辑:使用发布的事务启动与在查询的每个表上执行with(nolock)TRANSACTION_READ_UNCOMMITTED
相同。