reference/pdo/transactions.xml
969a4a70626497a200e4f2ffd883a29cbb228b33
969a4a70626497a200e4f2ffd883a29cbb228b33
...
...
@@ -27,12 +27,12 @@
27
27
connection. Auto-commit mode means that every query that you run has its
28
28
own implicit transaction, if the database supports it, or no transaction
29
29
if the database doesn't support transactions. If you need a transaction,
30
-
you must use the <function>PDO::beginTransaction</function> method to
30
+
you must use the <methodname>PDO::beginTransaction</methodname> method to
31
31
initiate one. If the underlying driver does not support transactions, a
32
32
PDOException will be thrown (regardless of your error handling settings:
33
33
this is always a serious error condition). Once you are in a transaction,
34
-
you may use <function>PDO::commit</function> or
35
-
<function>PDO::rollBack</function> to finish it, depending on the success
34
+
you may use <methodname>PDO::commit</methodname> or
35
+
<methodname>PDO::rollBack</methodname> to finish it, depending on the success
36
36
of the code you run during the transaction.
37
37
</para>
38
38
<warning>
...
...
@@ -48,6 +48,41 @@
48
48
a MySQL database.
49
49
</para>
50
50
</warning>
51
+
<warning>
52
+
<para>
53
+
<emphasis>Implicit Commits with DDL Statements:</emphasis>
54
+
Some databases automatically issue an
55
+
implicit <literal>COMMIT</literal> when a database definition language (DDL)
56
+
statement, such as <literal>DROP TABLE</literal> or <literal>CREATE TABLE</literal>,
57
+
is executed within a transaction. This means that any prior changes made within the
58
+
same transaction will be <emphasis>automatically committed</emphasis> and cannot
59
+
be rolled back.
60
+
</para>
61
+
<para>
62
+
<literal>MySQL</literal> and <literal>Oracle</literal> are example databases that
63
+
exhibit this behavior.
64
+
</para>
65
+
</warning>
66
+
<para>
67
+
<example>
68
+
<title>Implicit Commit Example</title>
69
+
<programlisting role="php">
70
+
<![CDATA[
71
+
<?php
72
+
$pdo->beginTransaction();
73
+
$pdo->exec("INSERT INTO users (name) VALUES ('Rasmus')");
74
+
$pdo->exec("CREATE TABLE test (id INT PRIMARY KEY)"); // Implicit COMMIT occurs here
75
+
$pdo->rollBack(); // This will NOT undo the INSERT/CREATE for MySQL or Oracle
76
+
?>
77
+
]]>
78
+
</programlisting>
79
+
</example>
80
+
</para>
81
+
<para>
82
+
<emphasis>Best Practice:</emphasis> Avoid executing DDL statements inside transactions
83
+
if using databases that enforce this behavior. If necessary, separate DDL operations
84
+
from transactional logic.
85
+
</para>
51
86
<para>
52
87
When the script ends or when a connection is about to be closed, if you
53
88
have an outstanding transaction, PDO will automatically roll it back.
...
...
@@ -59,7 +94,7 @@
59
94
<warning>
60
95
<para>
61
96
The automatic rollback only happens if you initiate the transaction via
62
-
<function>PDO::beginTransaction</function>. If you manually issue a
97
+
<methodname>PDO::beginTransaction</methodname>. If you manually issue a
63
98
query that begins a transaction PDO has no way of knowing about it and
64
99
thus cannot roll it back if something bad happens.
65
100
</para>
...
...
@@ -72,8 +107,8 @@
72
107
In addition to entering the basic data for that person, we also need to
73
108
record their salary. It's pretty simple to make two separate updates,
74
109
but by enclosing them within the
75
-
<function>PDO::beginTransaction</function> and
76
-
<function>PDO::commit</function> calls, we are guaranteeing that no one
110
+
<methodname>PDO::beginTransaction</methodname> and
111
+
<methodname>PDO::commit</methodname> calls, we are guaranteeing that no one
77
112
else will be able to see those changes until they are complete. If
78
113
something goes wrong, the catch block rolls back all changes made
79
114
since the transaction was started, and then prints out an error
80
115