MySQL
  Home arrow MySQL arrow Page 6 - Using Transactions In MySQL (Part 1)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Using Transactions In MySQL (Part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 55
    2003-11-03

    Table of Contents:
  • Using Transactions In MySQL (Part 1)
  • Money, Money, Money...
  • The Acid Test
  • Turning the Tables
  • A Question of Commitment
  • Rules of the Game
  • Artificial Intelligence
  • Time Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Transactions In MySQL (Part 1) - Rules of the Game


    (Page 6 of 8 )

    It is important to note that MySQL does not permit nested transactions. As in the example below, if you start a new transaction without ending the previous one with a COMMIT or ROLLBACK, MySQL will automatically commit the previous transaction's data to disk before beginning a new transaction.

    mysql> START TRANSACTION;
    Query OK, 0 rows affected (0.03 sec)

    mysql> INSERT INTO users (name, pass) VALUES ('john', PASSWORD('john'));
    Query OK, 1 row affected (0.57 sec)

    mysql> START TRANSACTION;
    Query OK, 0 rows affected (0.00 sec)

    mysql> SELECT * FROM users;
    +----+------+------------------+
    | id | name | pass |
    +----+------+------------------+
    | 3 | alan | 5af23f026beddb81 |
    | 4 | john | 2ca0ede551581d29 |
    +----+------+------------------+
    2 rows in set (0.26 sec)


    A number of other SQL commands also perform such an implicit COMMIT - dropping, creating and altering tables; dropping and creating indexes; and dropping and creating databases.

    That said, it is interesting to note that if you begin a transaction, but exit the session before issuing a COMMIT or ROLLBACK, MySQL does *not* perform an automatic COMMIT; instead, it issues a ROLLBACK. The following example demonstrates:

    [me@host]$ mysql
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 5 to server version: 4.0.12-max-debug

    Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

    mysql> USE master;
    Database changed

    mysql> BEGIN;
    Query OK, 0 rows affected (0.00 sec)

    mysql> SELECT * FROM users;
    +----+------+------------------+
    | id | name | pass |
    +----+------+------------------+
    | 3 | alan | 5af23f026beddb81 |
    | 4 | john | 2ca0ede551581d29 |
    +----+------+------------------+
    2 rows in set (0.00 sec)

    mysql> INSERT INTO users (name, pass) VALUES ('tim', PASSWORD('hoo'));
    Query OK, 1 row affected (0.02 sec)

    mysql> exit
    Bye


    Now, when you open a new session, and check the "users" table, you'll see that there is no record for Tim, since MySQL issued a ROLLBACK when the previous session exited.

    [me@host]$ mysql
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 6 to server version: 4.0.12-max-debug

    Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

    mysql> USE master;
    Database changed


    mysql> SELECT * FROM users;
    +----+------+------------------+
    | id | name | pass |
    +----+------+------------------+
    | 3 | alan | 5af23f026beddb81 |
    | 4 | john | 2ca0ede551581d29 |
    +----+------+------------------+
    2 rows in set (0.01 sec)


    This is of particular relevance to the next example - so flip the page and let's see how.

    More MySQL Articles
    More By icarus, (c) Melonfire


       · Thank you for this article. I wish I could look at Part 2 - but I cannot find it. ...
       · http://www.devshed.com/c/a/MySQL/Using-Transactions-In-MySQL-Part-2Aruna...
     

       

    MYSQL ARTICLES

    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT