MyTransaction :: MyISAM Transaction Emulation

Copyright © Deep Blue Sky Digital Limited 2010
Version 0.1.0
This software is developed by Deep Blue Sky, Web Design experts in Bath & Brisol

Synopsis

MyTransaction is a PHP library which provides software layer transaction emulation for the MyISAM storage engine. For background information please read this article - MyISAM Transactions on my blog.

License

MyTransaction is distributed under the MIT License which basically means you can do what you like with it so long as you don't remove our copyright notice.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Caveat Emptor

MyTransaction is a pretty experimental idea and it does not provide the sort of protection provided from a true transactional storage engine like InnoDB.

If you need transaction support in a production environment use InnoDB!

Installation

There are four stages to installation:

  1. Including the main library.
  2. Including a few methods (which we've provided) in your database model.
  3. Swapping out your mysql_query($query) for a new one.
There is an assumption that your software has a single point of access to the database. This will make installation easier.

1: Include transactions.php

Erm. Pretty self explanatory - you need to include transactions.php somewhere in your code, wherever's appropriate.

2: Include the transactions methods

In the file transaction_methods.php you'll find four methods. You need to drop these into your database library. These are the public methods which you'll be using to control the transaction system.

3: Replacing mysql_query($query)

Last but not least you need to replace your call(s) to mysql_query($query) for one that allows the transaction engine to take over if required.

The transaction engine will only grab INSERT, UPDATE and DELETE statements.

Somewhere in your database library you'll be making a call to mysql_query() with a query string and a resource_id. We expect this code to be:

$result = mysql_query( $queryString, $resourceId );

In the file transaction_switch.php you'll find a few lines of code to help you replace that code with a few lines that make a decision as to whether to just do that or, if you have a transaction running, to hand the query off to the transaction library to deal with on your behalf.

Usage:

The following assumes some stuff about your db library, which we've called "db".. This is the library where you did 2 & 3 above.

// assuming that somewhere you do:
$db = new db();
$db -> connect();

// start your transaction:
$transaction_id = $db -> start_transaction();

// now assuming you originall did this:

$db -> insert_statement('INSERT INTO foo (a,b) VALUES (1,2),(3,4)');
$db -> update_statement('UPDATE bar SET flibble = 101 WHERE wibble = 99');

// now finally you can make a decision about whether you really want to do that..

if ( $i_am_happy ) {
	$db -> commit_transaction ( $transaction_id );
} else {
	$db -> rollback_transcation ( $transcation_id );
}

Failure to commit

NB. If you don't explicitly commit your transactions the library will make every attempt to roll them back when the request finishes!

Getting help / reporting bugs / contibuting

All of the above is _very_ welcome! You can email us directly at open-source@icommunicate.co.uk.