There is no active transaction error

Asked

Viewed 1,040 times

0

I started studying PDO these days, and I started doing a CRUD, but when I give the Insert it shows the error:

There is no active transaction

I tried to see in other related questions, but did not understand anything.. (The connection is right, because the login is working..).

Method of class Methodospdo:

static function inserirCliente($nome, $sobrenome) {
    try {
        $con = ConnectionFactory::getConnection();
        $con->beginTransaction();
        $stmt = $con->prepare("INSERT INTO pessoa(nome, sobrenome) VALUES(:nome, :sobrenome)");
        $stmt->bindParam('nome', $nome);
        $stmt->bindParam('sobrenome', $sobrenome);
        if($stmt->execute()){
            $result = 'Cadastrado com sucesso';
        } else {
            $result = 'Erro ao cadastrar!';
            $con->rollBack();
        }
        $con->commit();
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    $con = null;
    return $result;
}

Form:

   if ($_POST) {
        $nome = $_POST['nome'];
        $sobrenome = $_POST['sobrenome'];
         $result = MetodosPDO::inserirCliente($nome, $sobrenome);
         echo $result;
    }
    ?>

    <form action="" method="post">
        <input type="text" name="nome" value="" /><br>
        <input type="text" name="sobrenome" value="" /><br>
        <input type="submit" value="Cadastrar" name="btn" /><br>
    </form>

1 answer

1

It turns out you’re calling the commit even if the rollback happens. When you commit or rollback the transaction ends, so the error you mention.

I commented on these points in your code and then left a suggestion for you.


static function inserirCliente($nome, $sobrenome) {
    try {
        $con = ConnectionFactory::getConnection();
        $con->beginTransaction(); // Iniciou a transação
        $stmt = $con->prepare("INSERT INTO pessoa(nome, sobrenome) VALUES(:nome, :sobrenome)");
        $stmt->bindParam('nome', $nome);
        $stmt->bindParam('sobrenome', $sobrenome);
        if($stmt->execute()){
            $result = 'Cadastrado com sucesso';
        } else {
            $result = 'Erro ao cadastrar!';
            $con->rollBack(); // Quando você dá o rollBack você finaliza a transação
        }
        $con->commit(); // Se já aconteceu o rollback, quando chegar aqui vai dar o erro que você mencionou
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    $con = null;
    return $result;

}




// Sugestão

static function inserirCliente($nome, $sobrenome) {

    $con = ConnectionFactory::getConnection();
    $con->beginTransaction(); 

    try {

        $stmt = $con->prepare("INSERT INTO pessoa(nome, sobrenome) VALUES(:nome, :sobrenome)");
        $stmt->bindParam('nome', $nome);
        $stmt->bindParam('sobrenome', $sobrenome);


        if($stmt->execute()){
            $result = 'Cadastrado com sucesso';     
            $con->commit();

        } else {
            $con->rollBack();
            $result = 'Erro ao cadastrar!';        
        }

    } catch (PDOException $e) {
        $con->rollBack();
        echo $e->getMessage();

    }
    $con = null;
    return $result; 

}
  • Thank you, you’ve given me a lot of doubt about that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.