Recipe 15.2. Performing Transactions Using SQL


Problem

You need to issue a set of statements that must succeed or fail as a unitthat is, you need to perform a transaction.

Solution

Manipulate MySQL's auto-commit mode to enable multiple-statement transactions, and then commit or roll back the statements depending on whether they succeed or fail.

Discussion

This recipe describes the SQL statements that control transactional behavior in MySQL. The immediately following recipes discuss how to perform transactions from within programs. Some APIs require that you implement transactions by issuing the SQL statements discussed in this recipe; others provide a special mechanism that enables transaction management without writing SQL directly. However, even in the latter case, the API mechanism will map program operations onto transactional SQL statements, so reading this recipe will give you a better understanding of what the API is doing on your behalf.

MySQL normally operates in auto-commit mode, which commits the effect of each statement immediately as soon as it executes. (In effect, each statement is its own transaction.) To perform a transaction, you must disable auto-commit mode, issue the statements that make up the transaction, and then either commit or roll back your changes. In MySQL, you can do this two ways:

  • Issue a START trANSACTION (or BEGIN) statement to suspend auto-commit mode, and then issue the statements that make up the transaction. If the statements succeed, record their effect in the database and terminate the transaction by issuing a COMMIT statement:

    mysql> CREATE TABLE t (i INT) ENGINE = InnoDB; mysql> START TRANSACTION; mysql> INSERT INTO t (i) VALUES(1); mysql> INSERT INTO t (i) VALUES(2); mysql> COMMIT; mysql> SELECT * FROM t; +------+ | i    | +------+ |    1 | |    2 | +------+ 

    If an error occurs, don't use COMMIT. Instead, cancel the transaction by issuing a ROLLBACK statement. In the following example, t remains empty after the transaction because the effects of the INSERT statements are rolled back:

    mysql> CREATE TABLE t (i INT) ENGINE = InnoDB; mysql> START TRANSACTION; mysql> INSERT INTO t (i) VALUES(1); mysql> INSERT INTO t (x) VALUES(2); ERROR 1054 (42S22): Unknown column 'x' in 'field list' mysql> ROLLBACK; mysql> SELECT * FROM t; Empty set (0.00 sec) 

  • Another way to group statements is to turn off auto-commit mode explicitly by setting the autocommit session variable to 0. After that, each statement you issue becomes part of the current transaction. To end the transaction and begin the next one, issue a COMMIT or ROLLBACK statement:

    mysql> CREATE TABLE t (i INT) ENGINE = InnoDB; mysql> SET autocommit = 0; mysql> INSERT INTO t (i) VALUES(1); mysql> INSERT INTO t (i) VALUES(2); mysql> COMMIT; mysql> SELECT * FROM t; +------+ | i    | +------+ |    1 | |    2 | +------+ 

    To turn auto-commit mode back on, use this statement:

    mysql> SET autocommit = 1;                   

Not Everything Can Be Undone

Transactions have their limits, because not all statements can be part of a transaction. For example, if you issue a DROP DATABASE statement, don't expect to restore the database by executing a ROLLBACK.





MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net