Saturday, September 15, 2012

Transaction in Zend Framework with MySQL Or MSSQL | SQL Server and , Using Transaction , Commit and Rollback in Zend PHP

Transaction  in   Zend Framework  with MySQL Or MSSQL | SQL Server  and , Using  Transaction  , Commit and Rollback in Zend PHP 

Basically, in our common case we want to get “all or nothing” constraint in our database transaction. Usually, in MYSQL the SQL syntax begining a transaction: BEGIN, COMMIT for committing the updates, ROLLBACK for undoing all the updates in the transaction. In Zend Framework, Zend Db interface has its own class methods for beginning, commiting and rollback transaction. Bassically, as follow.


Transactions in zend framework with mysql or mssql  

$db->beginTransaction(); //begin a transaction

try{

         $db->query(“….”); //query1

         $db->query(”…”); // query 2

        $db->commit();

}

catch(Exception $e)

{
         $db->rollback();

}


In general web development, transactions are unnecessary. If for some reason post in your CMS isn’t saved, you’ll write it again. It is a problem, but only annoying one, not a crucial one. But if you own a web shop, and someone create an order, pay for it, and you – owner of that store can’t see it because of database glitch, that would be a problem. Take a look at example above. I only first query in that transaction was executed, total amount of money from account would be decreased by 100, and that money would simply disappear (if we’re looking at your database). Or if only second was executed, account 2 would have 100 extra in its total sum – again virtually from nowhere.


Note: In MySQL, transaction works only on database tables that are built using InnoDB storage engine. By default, MySQL uses MyISAM engine to create new tables. The solution is to create whatever tables you want to use in a transaction to be built using InnoDB engine. If you already created tables with MyISAM, you can still change to InnoDB by changing the database schema.




Cheers !
Hope you got the Ans !
still having probs ? let me know by comments !

Know More About :
PHP Freelancing India

0 comments:

Post a Comment

Any Questions or Suggestions ?

About

Professional & Experienced Freelance Developer From India, Technologist, Software Engineer, internet marketer and Open Sources Developer with experience in Finance, Telecoms and the Media. Contact Me for freelancing projects.

Enter your email address:

Delivered by FeedBurner