Organizational Research By

Surprising Reserch Topic

don't roll back PDO Transactions ?


I am going through this tutorial about PDO and have come to the point about transactions. Skipping the connection parts, I have this php code:

try
{
    $db->beginTransaction();

    $db->exec('DROP TABLE IF EXISTS animals');

    $db->exec('CREATE TABLE animals ('
        .'animal_id MEDIUMINT(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,'
        .'animal_type VARCHAR(25) NOT NULL,'
        .'animal_name VARCHAR(25) NOT NULL)'
        .'ENGINE=INNODB');

    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("emu", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("funnel web", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("lizard", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("dingo", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("kangaroo", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("wallaby", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("wombat", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("koala", "bruce")');
    $db->exec('INSERT INTO animals (animal_type, animal_name) VALUES ("kiwi", "bruce")');

    $db->commit();

    echo 'Table re-created and data entered successfully.';
}
catch(PDOException $e)
{
    $db->rollback();

    echo $e->getMessage();
}
It runs great and like I thought it would, except if I put in an error somewhere. Like if I created a mistake in the fourth insert statement, I would find three animals in my database. But I thought things were supposed to be rolled back, meaning that I would find the database like it was before I ran this script.

Have I misunderstood something? What am I missing? Does the transaction and rollback functions do something else than what I think they should be doing? Is the drop and create statements "breaking" the transaction somehow? What's going on here?
asked Jun 1, 2015 in codeinither by rajesh
0 votes
25 views



Related Hot Questions



Government Jobs Opening


...