【发布时间】:2018-08-03 07:00:44
【问题描述】:
我使用的是 ORACLE 11g 版。
我想“同时”执行三个查询,并注意如果其中一个或多个查询失败,则必须将两个表都返回到以前的状态。这些查询是一项选择以了解所选行是否仍然可以执行操作,以及一项更新和一项插入以执行操作。
在我的情况下,我需要对同一锁定行进行更新(显然没有其他人应该能够对同一行执行操作),然后再插入另一个表,前提是选择查询的结果确认选定的行仍然可以选择执行该操作,因此查询将大致如下:
//this is the row I want to execute the action
$selectedIdFromTable1 = "1";
$query="SELECT attr1 FROM table1 WHERE attr1 = 'oldValueAttr1' AND id = selectedIdFromTable1";
$stmt = $this->oracleDB->prepare($query);
$stmt->bindValue(1, $attr1, "string");
$stmt->execute();
$result = $stmt->fetchColumn();
if($result->num_rows == 1){ //I'm still being able to do the action to the row because the row still having the oldValue
//So here the row must be locked to execute the update and the insert only once. Only one user should execute the update and the insert.
$query="UPDATE table1 SET attr1 = ? WHERE id == $selectedIdFromTable1";
$stmt = $this->oracleDB->prepare($query);
$stmt->bindValue(1, 'newValueAttr1', "string");
$stmt->execute();
$query="INSERT INTO table2 (attr2) VALUES (?)";
$stmt = $this->oracleDB->prepare($query);
$stmt->bindValue(1, 'newValueAttr2', "string");
$stmt->execute();
}
//here the lock can release the row for future actions (but not this one, because if any one tries the previous select should not find anymore the selected row)
我还使用绑定系统来更安全地发送变量。不确定是否会影响答案。
我很确定带有锁定行的事务是答案,如果是答案,我将非常感谢您提供与 Oracle 的事务示例以及这种情况示例的帮助。
所有这些都将在 Symfony 3.3 项目中进行。这最后的信息可能不是必需的,但由于不同的原因,事务代码必须在 symfony 项目中而不是在 oracle 数据库中。
非常感谢。
【问题讨论】:
-
@OldProgrammer 对不起,你能像上次一样给我一个答案吗?
标签: php oracle symfony oracle11g