Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is already an active transaction

Tags:

php

pdo

i have a code that use commit and rollback

$pdo = new dbpdo();
    $fields = ['id','last','first'];
    $values = [NULL,'asd','asd'];
    //$pdo->db->beginTransaction();
    try {
        $pdo->db->beginTransaction();
        //echo 'connected';
        //$pdo->InsertBatch('staff',implode(',',$data));
        //$pdo->InsertBatch('staff',implode(',',$fields), implode(',',$values));
        $pdo->InsertBatch('staff',$fields, $values);
        //$pdo->exec("INSERT INTO staff('id','first','last') VALUES ('NULL','asd','asd')");
        //$pdo->execute();
        $pdo->commit();
        //$message = 'success';
        echo 'Save';
    } catch (Exception $e) {
        //die("Unable to connect: " . $e->getMessage());
        $pdo->rollBack();
        //$message = 'sayop';
        //die("Unable to connect: " . $e->getMessage());
        echo "Failed: " . $e->getMessage();
    }

my question is . when i execute this . . is says. There is already an active transaction and data is has not save in database. . dbpdo class has extend PDO class.

like image 443
mics Avatar asked Sep 03 '25 16:09

mics


1 Answers

I got the same scenario here today... I was trying to insert some master-detail records at Firebird and getting There is already an active transaction error.

My solution was to enable and disable the autocommit option, try this:

$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 0)

before calling $pdo->db->beginTransaction()

And when you complete your transaction, re-enable autocommit:

$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 1)

Reference: http://php.net/manual/pt_BR/pdo.begintransaction.php#113602

like image 67
Fernando Mertins Avatar answered Sep 05 '25 06:09

Fernando Mertins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!