【问题标题】:Oracle Multiple queries with locked row具有锁定行的 Oracle 多个查询
【发布时间】: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


【解决方案1】:

如果您将使用 symfony,您很可能会使用 DBAL 连接。 交易按照its documentation中的描述处理

(在我看来,它更像是一种交易功能而不是锁定功能)

交易:

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

锁定不是由 DBAL 处理的

【讨论】:

  • 在我的项目中,我使用了两个数据库。一个使用 MySQL 的我处理抛出 Doctrine ORM 并且很高兴知道我可以使用 ORM 锁定但是......另一个数据库使用 Oracle 11g,这个数据库我使用 Symfony 的 DBAL 选项管理它......(不能使用实体srry ...我只能在Oracle中使用普通查询,因为公司想要这样),所以我需要使用带有DBAL连接的oracle语言来完成。所以你的回答可能对任何人都有帮助,但对我来说,我想没有......
  • @RicardEspinàsLlovet 没问题,这也是 DBAL 功能,请参阅我的更新答案
  • so...“锁定不是由 DBAL 处理的”-> 如果不是由 DBAL 处理,我想我可以通过来自 oracle 的查询来处理锁定点?
  • 不知道Oracle的锁定机制,抱歉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多