mysql transactions core php and cakephp
Today I came across mysql transactions here we will learn (mysql transactions core php and cakephp). it is really very important and in most cases there is no alternative to it. Most of the time we have to insert, update and delete in multiple tables while performing single process.
Do not consider this transaction the Bank transaction, this transaction is some thing else.
What is mysql transaction?
MySql or any other Database transaction is when we have to run multiple queries and we need to confirm all queries are executed successfully, even if one is failed than also revert other those are executed successfully. because than our data will not be consistent.
Example suppose we are developing student admission application, on admission process when we fill the form and click submit, we insert records into 3 tables, No.1- students table and No.2- we also insert a record in payments table. we have to make sure both insertions are done successfully. it will be our big loss if we admit a student in school and we do not charge admission fee. So we want our application to confirm both queried executed successfully else if first query executed successfully but second query got some error in this case also revert first query.
How we implement Transaction using PHP
it has 5 steps
1- try catch block so that we can throw error
2- Begin transaction
3- Inside try block run all queries and throw error on failure of each query
4- At end of try block commit the transaction, and redirect to success page
5- At catch block rollback the transaction and redirect to failure page with appropriate message
No we know what is Transaction and how to implement it, lets see code examples
Transactions example with Core PHP MySqli
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$db= new mysqli("localhost","root","","dbname"); if($db->connect_error){ echo "ERROR: (".$db->connect_errno.") ".$db->connect_error; exit(); } //transaction example try { //begin transaction $db->->autocommit(FALSE); if(!$db->query('insert into table-1 ....')){ throw new exception($db->error); } if(!$db->query('insert into table-2 ....')){ throw new exception($db->error); } //.....more queries comes here, if you have //commit transaction $db->commit(); header('Location:success.php'); } catch (Exception $e) { //rollback transaction $db->rollback(); header('Location:failure.php?msg='.$e->getMessage()); } |
it was so easy, now lets see transaction example with Cake PHP
Transactions example with Cake PHP
same logic and steps as defined above for core PHP, just syntax is changed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//get datasource and begin transaction $dataSource = $this->Student->getDataSource(); $dataSource->begin(); try{ if (!$this->Student->save($this->request->data['Client'])) { throw new exception(__('The Student could not be saved. Please, try again.')); } if (!$this->Payment->save($this->request->data['Client'])) { throw new exception(__('The Payment could not be saved. Please, try again.')); } //..... more queries comes here, if you have //commit transaction $dataSource->commit(); //msg and redirect $this->Flash->success(__('Admission has been done successfully.')); return $this->redirect(array('action' => 'index')); }catch(Exception $ex){ //rollback transaction $dataSource->rollback(); //error msg and here we do not redirect $this->Flash->error(__($ex->getMessage())); } |
this was how to implement transaction using cake php
Using MySql the table engine type must be InnoDB, because MyISAM engine typed tables do not support transactions.
thats all
Comments