11<?php
2// $conn instanceof Doctrine\DBAL\Connection
3$conn->beginTransaction(); // 0 => 1, "real" transaction started
4try {
5
6 ...
7
8 // nested transaction block, this might be in some other API/library code that is
9 // unaware of the outer transaction.
10 $conn->beginTransaction(); // 1 => 2
11 try {
12 ...
13
14 $conn->commit(); // 2 => 1
15 } catch (\Exception $e) {
16 $conn->rollBack(); // 2 => 1, transaction marked for rollback only
17 throw $e;
18 }
19
20 ...
21
22 $conn->commit(); // 1 => 0, "real" transaction committed
23} catch (\Exception $e) {
24 $conn->rollBack(); // 1 => 0, "real" transaction rollback
25 throw $e;
26}
272
283
294
305
316
327
338
349
3510
3611
3712
3813
3914
4015
4116
4217
4318
4419
4520
4621
4722
4823
4924
5025
5126
1
2<?php
3/* Démarre une transaction, désactivation de l'auto-commit */
4$dbh->beginTransaction();
5
6/* Modification du schéma de la base ainsi que des données */
7$sth = $dbh->exec("DROP TABLE fruit");
8$sth = $dbh->exec("UPDATE dessert
9SET name = 'hamburger'");
10
11/* On s'aperçoit d'une erreur et on annule les modifications */
12$dbh->rollBack();
13
14/* Le connexion à la base de données est maintenant de retour en mode auto-commit */
15?>
16
17
1
2<?php
3try {
4 $dbh = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2',
5 array(PDO::ATTR_PERSISTENT => true));
6 echo "Connecté\n";
7} catch (Exception $e) {
8 die("Impossible de se connecter : " . $e->getMessage());
9}
10
11try {
12 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
13
14 $dbh->beginTransaction();
15 $dbh->exec("insert into staff (id, first, last) values (23, 'Joe', 'Bloggs')");
16 $dbh->exec("insert into salarychange (id, amount, changedate)
17 values (23, 50000, NOW())");
18 $dbh->commit();
19
20} catch (Exception $e) {
21 $dbh->rollBack();
22 echo "Failed: " . $e->getMessage();
23}
24?>
25
26