ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:transactions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
userguide:transactions [2015/12/29 20:33] – [Nesting Transactions] mnewnhamv5:userguide:transactions [2020/08/26 10:09] (current) – [Usage] Add braces as suggested by @peterdd in #629 dregad
Line 1: Line 1:
 ======Transaction Handling In ADOdb ====== ======Transaction Handling In ADOdb ======
 ===== Overview ===== ===== Overview =====
-There are 2 ways of handling transactions in ADOdb, **Granular** and **Smart**. Each way holds benefits but they cannot be mixed. Each method comprises a series of methods.+There are 2 ways of overriding the default database transaction handling methods in ADOdb, **Granular** and **Smart**. Each way holds benefits but they cannot be mixed. Each method comprises a series of methods.
  
   * Granular transactions allow full control of individual commits   * Granular transactions allow full control of individual commits
   * Smart transactions are easier to control   * Smart transactions are easier to control
 +
 +-------------------------------------------
 +
 +===== Default Behaviour =====
 +<WRAP right info 300px>
 +MySQL myIsam tables are always granular, and ignore any transaction control methods coded. You cannot rollback or fail an update into that table type. Use InnoDB table types instead.
 +
 +This also applies to any sequences that might have been updated in the transaction, and are not subject to transactional rollback.
 +</WRAP>
 +If ADOdb transaction handling is not used, then the transaction handling method is controlled by whatever is set by the database. Possible methods are:
 +
 +  * Automatically creating granular updates
 +  * Automatically rolling back the transactions at the end of procedures
 +  * Failing unless a transaction handling method is assigned on connection
  
 ---------------------------------------------- ----------------------------------------------
 +
 ===== Granular Transactions ===== ===== Granular Transactions =====
 <WRAP right box> <WRAP right box>
 == Available Methods == == Available Methods ==
-[[reference:beginTrans()]]\\ +[[v5:reference:connection:begintrans|beginTrans()]]\\ 
-[[reference:rollbackTrans()]]\\ +[[v5:reference:connection:rollbacktrans|rollbackTrans()]]\\ 
-[[reference:completeTrans()]]+[[v5:reference:connection:completetrans|completeTrans()]]\\ 
 +== See Also == 
 +[[v5:reference:connection:transOff|Temporarily Disabling Transactions]]\\ 
 +[[v5:reference:connection:transCnt|Checking Transaction Status]]\\
 </WRAP> </WRAP>
 When using granular transactions, it is the duty of the programmer to check whether a transaction execution has succeeded or failed. This allows the code to branch as desired in the case of a transaction failure. When using granular transactions, it is the duty of the programmer to check whether a transaction execution has succeeded or failed. This allows the code to branch as desired in the case of a transaction failure.
Line 28: Line 46:
 $ok = $conn->execute($sql); $ok = $conn->execute($sql);
  
-if ($ok)+if ($ok) {
    /*    /*
    * The previous execution succeeded, do some more    * The previous execution succeeded, do some more
    */     */ 
    $ok = $conn->execute($sql2);    $ok = $conn->execute($sql2);
-else+else {
    /*    /*
    * Branch, do some other work, then return    * Branch, do some other work, then return
    */    */
-if (!$ok) +
 + 
 +if (!$ok) {
     /*     /*
-    * Test the last insertion, if not successful roll back the entire transaction scope+    * Test the last insertion, if not successful roll  
 +    * back the entire transaction scope
     */     */
     $conn->rollbackTrans();     $conn->rollbackTrans();
-else+else {
     /*     /*
     * Go ahead and commit     * Go ahead and commit
     */      */ 
     $conn->commitTrans();     $conn->commitTrans();
 +}
 </code> </code>
-==== Stacking Transactions ==== 
-Granular transactions cannot be stacked, i.e. each ''beginTrans()'' in a procedure must be followed by a ''rollbackTrans()'' or a ''commitTrans()'' 
 ----------------------------- -----------------------------
 ===== Smart Transactions ===== ===== Smart Transactions =====
 <WRAP right box> <WRAP right box>
 == Available Methods == == Available Methods ==
-[[reference:startTrans()]]\\ +[[v5:reference:connection:starttrans|startTrans()]]\\ 
-[[reference:failTrans()]]\\ +[[v5:reference:connection:failtrans|failTrans()]]\\ 
-[[reference:hasFailedTrans()]]\\ +[[v5:reference:connection:hasfailedtrans|hasFailedTrans()]]\\ 
-[[reference:completeTrans]]+[[v5:reference:connection:completetrans|completeTrans()]] 
 +== See Also == 
 +[[v5:reference:connection:transOff|Temporarily Disabling Transactions]]\\ 
 +[[v5:reference:connection:transCnt|Checking Transaction Status]]\\
 </WRAP> </WRAP>
-Smart Transactions are much easier to use in large procedures because you do not have to track the error status. You start a smart transaction by calling StartTrans():+Smart Transactions are much easier to use in large procedures because you do not have to track the error status. You start a smart transaction by calling startTrans():
  
 <code php> <code php>
Line 70: Line 93:
 * Execute some SQL * Execute some SQL
 */ */
-$conn->Execute($sql);+$conn->execute($sql);
  
 /* /*
 * Execute some more SQL * Execute some more SQL
 */ */
-$conn->Execute($Sql2);+$conn->execute($sql2);
  
 /* /*
-* Call completetrans. If any of the executions in the above code have failed, the transactions will roll back automatically+* Call completetrans. If any of the executions in the above  
 +code have failed, the transactions will roll back automatically
 */ */
 $conn->completeTrans(); $conn->completeTrans();
 </code> </code>
  
-CompleteTrans() detects when an SQL error occurs, and will Rollback/Commit as appropriate. +[[v5:reference:connection:completetrans|completeTrans()]] detects when an SQL error occurs, and will Rollback or Commit the transactions as appropriate. 
  
-You can force a rollback even if no error occured, using failTrans(). Note that the rollback is done in CompleteTrans().+You can force a rollback even if no transactional error has occured, using [[v5:reference:connection:failtrans|failTrans()]]. Note that the rollback is done in completeTrans(), so any updates/inserts done after ''failTrans()'' is called will also be rolled back.
  
 <code php> <code php>
Line 92: Line 116:
 $conn->execute($sql); $conn->execute($sql);
  
-if (!CheckRecords()) +if (!checkRecords()) 
     $conn->failTrans();     $conn->failTrans();
  
-$conn->Execute($Sql2);+/* 
 +* Even though failTrans was issued before this statement, 
 +* it will also be rolled back 
 +*/ 
 +$conn->execute($sql2);
  
-$conn->CompleteTrans();+$conn->completeTrans();
 </code> </code>
  
-You can also check if a transaction has failed, using HasFailedTrans(), which returns true if FailTrans() was called, or there was an error in the SQL execution. Make sure you call HasFailedTrans() before you call CompleteTrans(), as it is only works between StartTrans/CompleteTrans.+You can also check if a transaction has failed, using [[v5:reference:connection:hasfailedtrans|hasFailedTrans()]], which returns true
 +  * if [[v5:reference:connection:failtrans|failTrans()]] was called,  
 +  * There was an error in the SQL execution. This execution comprises all scenarios where the SQL statement was successfully issued, but failed to complete. This does not include scenarios where the construction of the statement failed, and an error occurred that was trapped by PHP error handling. 
 + 
 +Make sure you call ''hasFailedTrans()'' before you call ''completeTrans()'', as it is only works between startTrans/completeTrans.
  
  
Line 119: Line 151:
 */ */
 $conn->startTrans(); $conn->startTrans();
-if (!CheckRecords()) +if (!checkRecords()) 
     /*     /*
     * This is applied to the outer block     * This is applied to the outer block
Line 136: Line 168:
 $conn->completeTrans(); $conn->completeTrans();
 </code> </code>
- +==== Savepoints ==== 
 +Savepoints are currently not supported.
v5/userguide/transactions.1451417591.txt.gz · Last modified: 2017/04/21 11:39 (external edit)