symfony - 多重/连接交易原则 - Symfony
问题描述
我想在 Symfony 3 中使用 Doctrine 进行两个连续的事务。第一个事务没问题,但第二个事务似乎正在工作,因为 $insertedRows 总是得到 1 并且没有明显地进行回滚,但是在进行提交时似乎不是应用这最后一个插入。如果我搜索真实数据库中的行不存在。
我在这里留下一个我正在做的例子。
$db->beginTransaction();
$db->setAutoCommit(false);
//the first transaction it's to update a register with another num
try{
$num = $this->getNum($params);
if(!($num > 0)){ $db->rollBack();}
$updatedRows = $this->updateNum($params);
if($updatedRows === 0){ $db->rollBack();}
$db->commit();
} catch (\Exception $e) {
$db->rollBack();
throw $e;
}
//until here is ok. I can see the changes in the database
//this second transatcion I don't know why is not working.
$db->beginTransaction();
$db->setAutoCommit(false);
try{
$insertedRows = $this->insertNewRegister($params);
if($insertedRows === 0){ $db->rollBack();}
//the insertedRows is equal to 1 if I do a dump here, so it seems is inserting this new row
//but after the commit if I check this new row in the database is not there
$db->commit();
} catch (\Exception $e) {
$db->rollBack();
throw $e;
}
有人知道我在做什么错吗?也许任何人都可以给我看两个连续交易的例子。
解决方案
您可以解决删除第二个电话的问题
$db->beginTransaction();
您的第二次调用有问题
$db->beginTransaction();
是在新交易自动打开setAutoCommit(false)
后 设置的时候$db->commit();
,所以你不需要打开新交易。
您可以阅读文档教义项目。
推荐阅读
- ios - 如何在 Storyboard 中嵌套视图控制器
- java - 在 ComboBox 对象中检索数组对象
- docker - Docker swarm 健康检查失败杀死容器 - 退出 0
- macos - 我无法安装 pod
- javascript - Chrome 中 fetch() 和 setTimeout() 的执行优先级
- mysql - 我应该在 for 循环中将带有数组的对象作为属性插入到我的数据库中吗?
- html - 在相同的行高css上打印div
- c# - WinUI ProgressRing 不显示在 WASM (Uno 2.4) 上
- linux - 在规范文件中获取 RPM 文件位置
- math - 不能被给定素数整除的最大除数之和是多少