Link to home
Start Free TrialLog in
Avatar of jrm213jrm213
jrm213jrm213Flag for United States of America

asked on

PDO MySQL transaction not rolling back properly or not rolling back at all

Hi,

I have the following code that runs a bulk insert statement, I have it all wrapped in  a transaction but when the insert causes an error, I get left with records in the database, which there shouldn't be because I rollback the transaction. So I am not sure if it is because I am running the insert as a prepared statement or if something in my code is just off and I can't see it.

 //next run additions
    unset($values);
    if($additions_count > 0)
    {
    	   $conn = new connection();  	    
	   $conn->openPDODB();
	   $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	   
	    for($a = 0;$a<$additions_count;$a++)
	    {
	    	    if($additions[$a]->get_password() == ""){$additions[$a]->set_password(gen_password());}
	    	    $values[] = '("' . $additions[$a]->get_username() . '","' . $additions[$a]->get_password() . '","' . $additions[$a]->get_salt() . '",' . $additions[$a]->get_licensing_entity_license_id() . ',"' . date("Y-m-d h:m:s") . '",' . $additions[$a]->get_pwd_change_required() . ')';
	    }
	    
	    $strSQL = "Insert into `" . STUDENT_TABLE . "` (`username`,`password`,`salt`,`licensing_entity_license_id`,`date_created`,`require_password_change`) Values " . implode(',',$values);
	    $dbh->beginTransaction();
	    $stmt = $dbh->prepare($strSQL);
	    try
	    {
	    	    $stmt->execute();
	    }
	    catch (PDOException $e)
	    {
	    	    $dbh->rollBack();
	    	    $ec = $stmt->errorCode();   	    	    
	    	    cleanup_and_exit("Location:/eas/upload_student_file.php?err=$ec",$handle);
	    	    //$insert_exceptions[] = $additions[$a];
	    }
	    $dbh->commit();
	    $values = array();   
    }

Open in new window


I have a constraint on the table that username/password combination is unique. When I upload a file that violates that constraint in one of the rows, it handles the error and reports back the correct error, but I end up with a partial insert into the database that remains instead of being rolled back.
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

try moving the beginTransaction into the 'try' e.g
    try
    {
        $dbh->beginTransaction();
      $stmt->execute();
    }
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jrm213jrm213

ASKER

@DaveBaldwin you are probably correct, for some reason i thought the default was innodb for mysql 5+ but apparently it isn't or the database that I am working with was built in something earlier and the default was set to myIsam. I will change it and see what happens, but I am guessing it will solve the issue.
Thanks!
InnoDB is the new default storage engine starting with MySQL 5.6, I knew I had read that and got it in my head starting with 5, not 5.6