Oracle具有锁定行的多个查询

时间:2018-02-23 08:45:57

标签: php oracle symfony oracle11g

我使用的是ORACLE 11g版本。

我想同时执行三个查询""并注意如果一个或多个这些查询失败,则必须将两个表都返回到先前的状态。这些查询是一个选择,以知道所选行是否仍然可以进行操作,以及一个更新和一个插入来执行操作。

在我的情况下,我需要对同一个锁定的行进行更新(显然没有其他人应该能够对同一行执行操作)以及稍后在另一个表上插入,仅在select查询的结果时确认所选行仍然可以选择执行操作,因此查询将大致如下:

//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)

此外,我使用绑定系统更安全地发送变量。不确定If是否会影响答案。

我非常确定具有锁定行的事务是答案,如果答案是肯定的,我将非常感谢您收到有关Oracle事务示例的帮助,并举例说明情况。

所有这一切都将在Symfony 3.3项目中进行。可能不需要这个最后的信息,但是事务代码必须在symfony项目中而不是在oracle数据库中,原因不同。

非常感谢。

1 个答案:

答案 0 :(得分:1)

如果您将使用symfony,您很可能会使用DBAL连接。 交易按照its documentation

中的描述进行处理

(对我来说,这似乎是一个交易功能而不是锁定功能)

交易:

$conn->beginTransaction();
try{
    // do stuff
    $conn->commit();
} catch (\Exception $e) {
    $conn->rollBack();
    throw $e;
}

DBAL不处理锁定