zend 适配器 beginTransaction() 和 commit() 失败
我面临一个奇怪的问题
Zend_DB_Adapter 的 beginTrasaction() 和 commit() 方法似乎没有按预期工作。我有 INSERT 语句(在 FOR 循环中)包含在 beginTrasaction() 和 commit() 方法中。但即使发生如下错误,我仍然看到已经插入了一些行,而我预计由于发生错误而不会发生提交。我无法理解为什么。有人可以帮忙吗?谢谢。
SQLSTATE[23000]:违反完整性约束:1062 键“PRIMARY”的重复条目“0”
代码块如下:
**$localDB->beginTransaction();**
try{
echo $localDB->isConnected();
$localDB->query("TRUNCATE TABLE $this->dbTable");
**foreach ($rowSet as $row){**
foreach ($row as $key=>$value){
$localRow[$this->columnMap[$key]] =$value;
}
**$localDB->insert($this->dbTable,$localRow);**
}
$localDB->commit();
}
catch (Exception $e){
$localDB->rollBack();
echo $e->getMessage();
}
I am facing a strange issue
The Zend_DB_Adapter's beginTrasaction() and commit() methods don't seem to be working as expected. I have INSERT statements (in a FOR LOOP) enclosed in beginTrasaction() and commit() methods. But even when error occurs like below, I still see some rows inserted already, while I was expecting the commiting not to happen since errors occured. I am not able to comprehend why. Can someone help . thanks.
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'
code block like:
**$localDB->beginTransaction();**
try{
echo $localDB->isConnected();
$localDB->query("TRUNCATE TABLE $this->dbTable");
**foreach ($rowSet as $row){**
foreach ($row as $key=>$value){
$localRow[$this->columnMap[$key]] =$value;
}
**$localDB->insert($this->dbTable,$localRow);**
}
$localDB->commit();
}
catch (Exception $e){
$localDB->rollBack();
echo $e->getMessage();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TRUNCATE TABLE
将导致隐式commit 将结束当前事务。1) 将
TRUNCATE TABLE
放在beginTransaction()
之前。2)
DELETE FROM
应该是事务保存(The
TRUNCATE TABLE
will cause an implicit commit wich will end the current transaction.1) Put
TRUNCATE TABLE
beforebeginTransaction()
.2)
DELETE FROM
should be transaction save (but slower)