这件事情是昨晚发生在我身上的。我非常熟悉错误的性质,但我仍然无法弄清楚是什么原因造成的。我可能有预感,但我不确定。我将从一些基本的应用信息开始:
我的应用有 3 个实体:Loan
、SystemPage
和 TextPage
。每当有人添加贷款时,一个或多个系统页面就会被添加到数据库中。基本上,它是这样的:
if ( $form->isValid()){
$this->em->getConnection()->beginTransation();
$this->em->persist($loan);
$this->em->flush();
while ($someCondition){
$page = new SystemPage();
//... Fill the necessary data into page
$page->setObject($loan);
$this->em->persist($page);
}
$this->em->flush();
$this->em->getConnection()->commit();
}
请忽略潜在的错别字,我是靠内存写的
实体 Loan
被映射到表 loans
并且 SystemPage
被映射(通过继承映射)到 system_pages
并且base_pages
。后两者都有 id
字段设置为 AUTO_INCREMENT
。
我的直觉:还有另一个名为 text_pages
的表。考虑到一方面是 text_pages
和 base_pages
,另一方面是 system_pages
和 base_pages
共享 ID,我认为它很容易导致这种情况:
User1: Create BasePage, acquire autoincrement ID (value = 1)
User2: Create BasePage, acquire autoincrement ID (value = 1)
User1: Create TextPage, use the ID from step 1
User2: Create SystemPage, use the ID from step 2
这个理论的两个问题:
- 交易。这就是我首先使用它们的原因
- 在出错时没有其他用户在应用上进行其他事件
重要:稍等一分钟,重新提交就OK了。
这会不会是一些奇怪的 MySQL
事务隔离错误?任何提示将不胜感激...
编辑:
数据库模式的一部分:
请忽略塞尔维亚语列名
最佳答案
flush() 操作会在单个事务中刷新所有更改,因此这里有冗余代码...
您没有说明是否可以重现此错误,如果您可以提供数据库模式会很方便。
关于php - 交易期间 key '...' 的重复条目 'PRIMARY',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32364189/