duanmoen784988 2010-06-22 15:59
浏览 55
已采纳

何时在PHP执行Oracle PL / SQL-OCI8时会执行自动回滚?

I have PHP code that execute a stored procedure 10 times. If one stored procedure call fails, it should continue on, and at the end commit the transaction.

It basically looks like this:

$connection = getConn();

foreach($row as $i=>$j) {
  $SQL = "BEGIN MYPROC.EXECUTE(:VAL1, :VAL2); END;";
  $statement = OCIParse($connection, $SQL);

  oci_bind_by_name($statement, 'VAL1', $row[i]['FIRSTVAL']);
  oci_bind_by_name($statement, 'VAL2', $row[i]['SECONDVAL']);

  $success = @OCIExecute($statement, OCI_DEFAULT);
  if(!$success) {
    print 'Exception in stored proc call';
  }
  else {
    print 'Success';
  }

}
oci_commit($connection);

My question is, if there is an exception raised in, say, the 5th stored proc call, will that roll back all the stored proc calls up to that point?

  • 写回答

3条回答 默认 最新

  • dry69034 2010-06-22 16:04
    关注

    As long as each procedure is executed in the same session, and none of them issue a commit, then the changes they make can be rolled back. You should open the connection outside the loop, then do all your work within that. As it stands now, you're connecting each time through the loop, which is inefficient and won't allow what you want to do. You should also take the commit statement outside the loop.

    Something like this, perhaps:

    $SQL = "BEGIN MYPROC.EXECUTE(:VAL1, :VAL2); END;";
    $connection = getConn();
    $statement = OCIParse($connection, $SQL);
    
    foreach($row as $i=>$j) {
    
      oci_bind_by_name($statement, 'VAL1', $row[i]['FIRSTVAL']);
      oci_bind_by_name($statement, 'VAL2', $row[i]['SECONDVAL']);
    
      $success = @OCIExecute($statement, OCI_DEFAULT);
      if(!$success) {
        print 'Exception in stored proc call';
        oci_rollback($connection);
        exit processing here... 
      }
      else {
        print 'Success';
      }
    }
    oci_commit($connection);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退