PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

PDO::setAttribute> <PDO::quote
Last updated: Fri, 05 Sep 2008

view this page in

PDO::rollBack

(PHP 5 >= 5.1.0, PECL pdo:0.1-1.0.3)

PDO::rollBack Annule une transaction

Description

bool PDO::rollBack ( void )

Annule la transaction courante, initié par la fonction PDO::beginTransaction(). C'est une erreur que d'appeler cette méthode s'il n'y a aucune transaction active.

Si la base de données est en mode autocommit, cette fonction restaurera le mode autocommit après l'annulation de la transaction.

Quelques bases de données, dont MySQL, exécuteront automatiquement un COMMIT lorsqu'une requête de définition de langage de base de données (DDL) comme DROP TABLE ou CREATE TABLE est exécutée dans une transaction. Ce COMMIT implicite vous empêchera d'annuler toutes autres modifications faites dans cette transaction.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.

Exemples

Exemple #1 Annulation d'une transaction

L'exemple suivant commence une transaction et exécute deux requêtes qui modifient la base de données avant d'annuler les modifications. Sur MySQL, cependant, la requête DROP TABLE validera automatiquement la transaction, donc, aucune des modifications de la transaction ne sera annulée.

<?php
/* Début d'une transaction, désactivation du mode autocommit */
$dbh->beginTransaction();

/* Modifie le schéma de la base de données ainsi que des données */
$sth $dbh->exec("DROP TABLE fruit");
$sth $dbh->exec("UPDATE dessert
    SET name = 'hamburger'"
);

/* On s'aperçoit d'une erreur et on annule les modifications */
$dbh->rollBack();

/* La connexion à la base de donnés revient en mode autocommit */
?>



PDO::setAttribute> <PDO::quote
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
PDO::rollBack
brian at diamondsea dot com
25-Mar-2008 04:19
Here is a way of testing that your transaction has started when using MySQL's InnoDB tables.  It will fail if you are using MySQL's MyISAM tables, which do not support transactions but will also not return an error when using them.

<?
// Begin the transaction
$dbh->beginTransaction();

// To verify that a transaction has started, try to create an (illegal for InnoDB) nested transaction.
//    If it works, the first transaction did not start correctly or is unsupported (such as on MyISAM tables)
try {
   
$dbh->beginTransaction();
    die(
'Cancelling, Transaction was not properly started');
} catch (
PDOException $e) {
    print
"Transaction is running (because trying another one failed)\n";
}
?>
JonasJ
08-Jan-2008 10:59
Just a quick (and perhaps obvious) note for MySQL users;

Don't scratch your head if it isn't working if you are using a MyISAM table to test the rollbacks with.

Both rollBack() and beginTransaction() will return TRUE but the rollBack will not happen.

Convert the table to InnoDB and run the test again.
linfo2003 at libero dot it
30-May-2007 04:07
Since "It is an error to call this method if no transaction is active", it could be useful (even if not indispensable) to have a method which returns true if a transaction is active.

try {
    $dbh->beginTransaction();
    ...
} catch (PDOException $e) {
    if ($dbh->isTransactionActive())  // this function does NOT exist
        $dbh->rollBack();
    ...
}

In the meanwhile, I'm using this code:

    ...
} catch (PDOException $e) {
    try { $dbh->rollBack(); } catch (Exception $e2) {}
    ...
}

It's not so chic, but it works fine.
emalinks at gmail dot com
14-Apr-2007 10:09
<?php
//run this on your latest PHP let me know if it doesn't fail
//to test this program u must run it twice at the same time in ie. two terminals
//this program suposedly creates a new dbase with table name NodeNames and one field in it named NodeName
//it then begins a transaction then attempts to read an element 'zTest' of field 'NodeName' which obv. doesn't exist, ignoring the returned errors
//then it writes it(since it wasn't there)
//then decides to rollBack the transaction and eventually try a new one
//because rollBack doesn't really work(apparently) for some unknown reason, beginTransaction fails saying 'There is already an active transaction'
$db = new PDO('sqlite:demlinks6.3sql',''/*user*/,''/*pwd*/);

$db->exec('CREATE TABLE \'NodeNames\' ("NodeName" VARCHAR(10));');

$db->beginTransaction();

$getter="zTest";
$pgn = $db->prepare('SELECT * FROM \'NodeNames\' WHERE "NodeName" = :node13');
$pgn->bindParam(":node13", $getter, PDO::PARAM_STR);
//read
$pgn->execute();//execute above SELECT
$ar=$pgn->FetchAll();//get array of results

$writter="zTest";
$pnn=$db->prepare('INSERT INTO \'NodeNames\' ("NodeName") VALUES (:node14)');
$pnn->bindParam(":node14", $writter, PDO::PARAM_STR);
//write
$pnn->execute();//write it!

echo "waiting...";
usleep(2000000);
echo
"done\n";
$db->rollBack();//this doesn't do it's job
$db->beginTransaction();//here it fails, when running this program twice at the same time; 'There is already an active transaction'
//unreachable:
$db->commit();
?>

PDO::setAttribute> <PDO::quote
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites