MySQL 5 supports several storage engines (InnoDB, MyISAM, NDB, Merge, Blackhole, Falcon, ...). The common are InnoDB and MyISAM. And this fact has some reason and implications to use one or other feature. MyISAM doesn't support transaction-safe tables, while InnoDB does it. So this is a pre-requirement to go ahead: we'll use InnoDB storage engine.
For more details, please see chapter 14 in MySQL 5.1 manual.
Here we'll describe the main SQL tools that will allow us to make our transaction-safe statements.
MySQL starts with AUTOCOMMIT mode on by default. This mean all changes are saved onebyone, in order they are called. So, we must use:
BEFORE use the tools described above. The difference between (1) and (2) is using (2) the AUTOCOMMIT mode will be enabled after end the transaction (a COMMIT or a ROLLBACK).
There are some statements that cannot be rolled back. I'll talk about this later.
When a SAVEPOINT is created with the same name as an older one, the older one is deleted.
Some examples of transaction-safe statements using our SQL tools.
Line-by-line this doesn't seems useful, but consider using these tools inside SQL functions, stored procedures, triggers, scheduled events, subjects of next articles about databases I'll publish.
Another interesting possibility is use transactions in scripts of other programming languages, like C, PHP, Java, Python,..., that allows you iterate upon queries and work easier. This article talk about this from now.
PHP has a class that directly supports transactions, the mysqli class. All other restrictions as use InnoDB tables remains valid.
The main idea is work with flags. Let's suppose you have a "join now" form, and the process consists of:
It would be pretty good if none of these steps are made if one of they fail, mainly if the confirmation email wasn't sent. A simple XML log file is a good idea to detect tries of code injection, and it is as important as your database.
See the script below:
Alright, it is a simplified example, but it ilustrates how to use transactions.
IMPORTANT: There aren't directly support to SAVEPOINT features in this class. But you can use it like an normal query.
There are some SQL statements that cannot be undone with ROLLBACK, or ROLLBACK TO SAVEPOINT statements, because they implicitly cause a COMMIT, and you must avoid use they inside your transactions.
Technically speaking, these statements are from DDL (Data Definition Language) group, i. e. statements used for alter the structure of the database and its components (tables, functions, stored procedures, triggers, scheduled events, etc...).
These statements are listed below (similar or aliases are excluded, but they have the same effect):
To this article was used a MySQL 5.0, Apache 2.0 servers and PHP 5.0.
This article was originally written by Davis L. P. Peixoto.
Date: May 04, 2007
Todo o material divulgado neste site está licenciado sob Licença Creative Commons, e têm caráter meramente informativo e didático, não sendo o autor responsável pelo mal uso das informações nele contido.
Última atualização em: 05/2007 | São Paulo, Brasil | Tem alguma sugestão ou crítica ?