Skip to content

ORACLE trans commit when got PDOException in transaction with bath insert which callback ID #1133

@MutionHu

Description

@MutionHu

Information

  • Version of Medoo: [2.1.12]
  • Version of PHP: [8.2]
  • Type of Database: [oracle]
  • System: [Linux|Windows|Mac]

Describe the Problem
Batch insert in Oracle transaction, and get the automatically generated ID when inserting. If PDOException occurs, the normal SQL before the abnormal SQL will still be commit transaction, because the third parameter - primary key, such as ID, is passed in the insert function. Because in the final exec function, if the third parameter is a callback, it will be automatically submitted.

Code Snippet
The detail code you are using that causes the problem:
note:the field ID is primary key,generated by oracle

try {
      $database->pdo->beginTransaction();
       
      $database->insert("account", [
	      "user_name" => "foo",
	      "email" => "foo@bar.com",
	      "age" => 25
      ],'ID');
      $database->insert("account", [
	      "ID" => "",
             "user_name" => "Doo",
	      "email" => "Doo@bar.com",
	      "age" => 20
      ],'ID');
       
      /* Commit the changes */
      $database->pdo->commit();
 }catch (PDOException $e){
   /* Recognize mistakes and roll back changes */
   $database->pdo->rollBack();
    echo $e->getMessage();
}

the exec code in Medoo

        if (is_callable($callback)) {
            $this->pdo->beginTransaction();
            $callback($statement);
            $execute = $statement->execute();
            $this->pdo->commit();
        } else {
            $execute = $statement->execute();
        }

Expected Behavior
the foo record will not commit;

Actual Behavior
the foo record will commit;

how to fix

        if (is_callable($callback)) {
            if (!$this->pdo->inTransaction()) {
                        $this->pdo->beginTransaction();
                        $callback($statement);
                        $execute = $statement->execute();
                        $this->pdo->commit();
            }else{
                        $callback($statement);
                        $execute = $statement->execute();     
            }
        } else {
            $execute = $statement->execute();
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions