(mysql.info.gz) InnoDB transactions with different APIs
Info Catalog
(mysql.info.gz) Using InnoDB tables
(mysql.info.gz) Using InnoDB tables
(mysql.info.gz) Converting tables to InnoDB
15.7.1 How to Use Transactions in `InnoDB' with Different APIs
--------------------------------------------------------------
By default, each client that connects to the MySQL server begins with
autocommit mode enabled, which automatically commits every SQL
statement you run. To use multiple-statement transactions, you can
switch autocommit off with the SQL statement `SET AUTOCOMMIT = 0' and
use `COMMIT' and `ROLLBACK' to commit or roll back your transaction.
If you want to leave autocommit on, you can enclose your transactions
between `START TRANSACTION' and `COMMIT' or `ROLLBACK'. Before MySQL
4.0.11, you have to use the keyword `BEGIN' instead of `START
TRANSACTION'. The following example shows two transactions. The first
is committed and the second is rolled back.
shell> mysql test
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 3.23.50-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> CREATE TABLE CUSTOMER (A INT, B CHAR (20), INDEX (A))
-> TYPE=InnoDB;
Query OK, 0 rows affected (0.00 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO CUSTOMER VALUES (10, 'Heikki');
Query OK, 1 row affected (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
mysql> SET AUTOCOMMIT=0;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO CUSTOMER VALUES (15, 'John');
Query OK, 1 row affected (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM CUSTOMER;
+------+--------+
| A | B |
+------+--------+
| 10 | Heikki |
+------+--------+
1 row in set (0.00 sec)
mysql>
In APIs like PHP, Perl DBI/DBD, JDBC, ODBC, or the standard C call
interface of MySQL, you can send transaction control statements such as
`COMMIT' to the MySQL server as strings just like any other SQL
statements such as `SELECT' or `INSERT'. Some APIs also offer separate
special transaction commit and rollback functions or methods.
Info Catalog
(mysql.info.gz) Using InnoDB tables
(mysql.info.gz) Using InnoDB tables
(mysql.info.gz) Converting tables to InnoDB
automatically generated byinfo2html