MySQL
  Home arrow MySQL arrow Page 9 - Using Transactions In MySQL (Part 2)
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 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    2003-12-22

    Table of Contents:
  • Using Transactions In MySQL (Part 2)
  • Isolating Yourself
  • The Three R’s
  • Peeping Tom
  • Locks and Keys
  • Nothing Like the Real Thing
  • Holding Pattern
  • Timberrrrrrrrrr!
  • Perl of Wisdom
  • End Work

  • 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 2) - Perl of Wisdom


    (Page 9 of 10 )

    You should now know a little bit about how transactions work at the SQL command prompt. This is great for learning purposes; however, in the real world, it's unlikely that your application will be interacting with MySQL through a command prompt. With that in mind, let's look at a simple application (written in Perl) that implements a transaction using Perl DBI methods.

    #!/usr/bin/perl
    # load module
    use DBI();
    # variables for the transaction
    # assume these have come from standard input
    $name = "ian";
    $pass = "storm";
    @groups = ("hr", "admin");
    $mhost = "my.pop.server";
    $muser = "my.name";
    $mpass = "my.pass";
    # connect
    my $dbh = DBI->connect
    ("DBI:mysql:database=master;host=192.168.0.241",
    "root", "secret", {RaiseError => 1, AutoCommit => 0});
    # place the transaction in an eval{} block
    # so that errors can be trapped for rollback
    eval
    {
    # insert user record
    $dbh->do("INSERT INTO users (name, pass) 
    VALUES ('$name', '$pass')");
    # get ID
    $id = $dbh->{'mysql_insertid'};
    # insert group memberships
    foreach $g (@groups)
    {
    $dbh->do("INSERT INTO groups 
    (uid, grp) VALUES ('$id', '$g')");
    }
    # insert mailbox data
    $dbh->do("INSERT INTO mailboxes (uid, host, 
    mboxname, mboxpass) VALUES ('$id', 
    '$mhost', '$muser', '$mpass')");
    # got this far means no errors
    # commit
    $dbh->commit();
    };
    # if any errors
    # rollback
    if ($@)
    {
    print "Transaction aborted: $@";
    $dbh->rollback();
    }
    # clean up
    $dbh->disconnect();
    

    The transaction in question here is the same as before - adding a user to the system - only the method differs. The first step is to connect to the database using the connect() method. Note the addition of the AutoCommit parameter to connect() - as explained in the previous segment of this tutorial, this tells MySQL to turn off automatic commits, and turns every query into a transaction that must be explicitly committed in order for it to be saved.

    Once a connection has been opened, standard INSERT statements are used to insert the new user's data into the system. These statements are enclosed in an eval{} exception handling block, so that errors, if any, are trapped and escalated upwards to the main program. An error in the eval{} block will be stored in the special $@ variable, and caught by the following "if" block, which rolls the entire transaction back with rollback(). If there are no errors, the transaction will be committed with commit().

    More MySQL Articles
    More By icarus, (c) Melonfire


     

       

    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