僵局返回错误1213您应该在客户端进行处理
注意,僵局和锁定等待是不同的事情。在僵局中,没有“失败”的交易:他们都是有罪的。不能保证将要回去哪一个。 您必须使用
rollback
$retry = 0;
$done = false;
$this->entityManager->getConnection()->beginTransaction(); // suspend auto-commit
while (!$done and $retry < 3) {
try {
$this->entityManager->flush();
$this->entityManager->getConnection()->commit(); // commit if succesfull
$done = true;
} catch (\Exception $e) {
$this->entityManager->getConnection()->rollback(); // transaction marked for rollback only
$retry++;
}
}
这是我如何处理与SF2.7和学说2.4.7:
重试交易失败交易的方式:
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManager;
class Foo
{
/**
* @var Registry
*/
protected $doctrine;
public function __construct(Registry $registry)
{
$this->doctrine = $registry;
}
protected function doSomething($entity, $attempt)
{
$em = $this->getEntityManager();
$conn = $em->getConnection();
try{
$conn->beginTransaction();
$entity->setBar("baz");
$em->flush();
$conn->commit();
} catch(\PDOException $e){
$conn->rollBack();
$attempt++;
if($attempt <= 3){
$this->doSomething($repayment, $attempt);
}
}
}
/**
* @return EntityManager
*/
protected function getEntityManager()
{
/** @var EntityManager $em */
$em = $this->doctrine->getManager();
if(!$em->isOpen()){
$this->doctrine->resetManager();
$em = $this->doctrine->getManager();
}
return $em;
}
}
重新交易片段
循环正确处理
代码EntityManager
Interface
Connection
通过构造函数
Connection
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
获得连接为$this->entityManager->getConnection()
如果您不使用ORM,然后使用它会自动管理僵局的情况。