How perform lockmode:none on associations (join) with Doctrine2 and SQL Server

隐身守侯 提交于 2019-12-22 12:38:14

问题


I'm trying to append the With(nolock) hint to my Queries using Doctrine2. I tried using the ->setLockMode(LockMode::NONE)->getSQL(); but it didn't work as expected, it added the WITH(NOLOCK) just to the (first) table in the FROM clause. I'll explain:

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

Expected:

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`

What I truly got:

SELECT c0_.prop1, c0_.prop2, c1_.prop1, c1_.prop2 
FROM examples c0_ WITH(NOLOCK) 
INNER JOIN owners c1_ ON c1_.id= c0_ownerId`

Possible Solution:

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

But I'm not sure if it's the same to do the with(nolock) than using the READ_UNCOMMITTED isolation level.

Edit: Starting the transaction as posted with the TRANSACTION_READ_UNCOMMITTED is the same as doing the with(nolock) on every table of the query.

来源:https://stackoverflow.com/questions/35431825/how-perform-lockmodenone-on-associations-join-with-doctrine2-and-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!