Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MutionHu opened this issue Dec 27, 2024 · 0 comments

Comments

@MutionHu
Copy link

MutionHu commented Dec 27, 2024

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" => "[email protected]",
	      "age" => 25
      ],'ID');
      $database->insert("account", [
	      "ID" => "",
             "user_name" => "Doo",
	      "email" => "[email protected]",
	      "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();
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant