Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############ |
| 2 | Transactions |
| 3 | ############ |
| 4 | |
| 5 | CodeIgniter's database abstraction allows you to use transactions with |
| 6 | databases that support transaction-safe table types. In MySQL, you'll |
| 7 | need to be running InnoDB or BDB table types rather than the more common |
| 8 | MyISAM. Most other database platforms support transactions natively. |
| 9 | |
| 10 | If you are not familiar with transactions we recommend you find a good |
| 11 | online resource to learn about them for your particular database. The |
| 12 | information below assumes you have a basic understanding of |
| 13 | transactions. |
| 14 | |
| 15 | CodeIgniter's Approach to Transactions |
| 16 | ====================================== |
| 17 | |
| 18 | CodeIgniter utilizes an approach to transactions that is very similar to |
| 19 | the process used by the popular database class ADODB. We've chosen that |
| 20 | approach because it greatly simplifies the process of running |
| 21 | transactions. In most cases all that is required are two lines of code. |
| 22 | |
| 23 | Traditionally, transactions have required a fair amount of work to |
| 24 | implement since they demand that you to keep track of your queries and |
| 25 | determine whether to commit or rollback based on the success or failure |
| 26 | of your queries. This is particularly cumbersome with nested queries. In |
| 27 | contrast, we've implemented a smart transaction system that does all |
| 28 | this for you automatically (you can also manage your transactions |
| 29 | manually if you choose to, but there's really no benefit). |
| 30 | |
| 31 | Running Transactions |
| 32 | ==================== |
| 33 | |
| 34 | To run your queries using transactions you will use the |
| 35 | $this->db->trans_start() and $this->db->trans_complete() functions as |
| 36 | follows:: |
| 37 | |
| 38 | $this->db->trans_start(); $this->db->query('AN SQL QUERY...'); $this->db->query('ANOTHER QUERY...'); $this->db->query('AND YET ANOTHER QUERY...'); $this->db->trans_complete(); |
| 39 | |
| 40 | You can run as many queries as you want between the start/complete |
| 41 | functions and they will all be committed or rolled back based on success |
| 42 | or failure of any given query. |
| 43 | |
| 44 | Strict Mode |
| 45 | =========== |
| 46 | |
| 47 | By default CodeIgniter runs all transactions in Strict Mode. When strict |
| 48 | mode is enabled, if you are running multiple groups of transactions, if |
| 49 | one group fails all groups will be rolled back. If strict mode is |
| 50 | disabled, each group is treated independently, meaning a failure of one |
| 51 | group will not affect any others. |
| 52 | |
| 53 | Strict Mode can be disabled as follows:: |
| 54 | |
| 55 | $this->db->trans_strict(FALSE); |
| 56 | |
| 57 | Managing Errors |
| 58 | =============== |
| 59 | |
| 60 | If you have error reporting enabled in your config/database.php file |
| 61 | you'll see a standard error message if the commit was unsuccessful. If |
| 62 | debugging is turned off, you can manage your own errors like this:: |
| 63 | |
| 64 | $this->db->trans_start(); $this->db->query('AN SQL QUERY...'); $this->db->query('ANOTHER QUERY...'); $this->db->trans_complete(); if ($this->db->trans_status() === FALSE) { // generate an error... or use the log_message() function to log your error } |
| 65 | |
| 66 | Enabling Transactions |
| 67 | ===================== |
| 68 | |
| 69 | Transactions are enabled automatically the moment you use |
| 70 | $this->db->trans_start(). If you would like to disable transactions you |
| 71 | can do so using $this->db->trans_off():: |
| 72 | |
| 73 | $this->db->trans_off() $this->db->trans_start(); $this->db->query('AN SQL QUERY...'); $this->db->trans_complete(); |
| 74 | |
| 75 | When transactions are disabled, your queries will be auto-commited, just |
| 76 | as they are when running queries without transactions. |
| 77 | |
| 78 | Test Mode |
| 79 | ========= |
| 80 | |
| 81 | You can optionally put the transaction system into "test mode", which |
| 82 | will cause your queries to be rolled back -- even if the queries produce |
| 83 | a valid result. To use test mode simply set the first parameter in the |
| 84 | $this->db->trans_start() function to TRUE:: |
| 85 | |
| 86 | $this->db->trans_start(TRUE); // Query will be rolled back $this->db->query('AN SQL QUERY...'); $this->db->trans_complete(); |
| 87 | |
| 88 | Running Transactions Manually |
| 89 | ============================= |
| 90 | |
| 91 | If you would like to run transactions manually you can do so as follows:: |
| 92 | |
| 93 | $this->db->trans_begin(); $this->db->query('AN SQL QUERY...'); $this->db->query('ANOTHER QUERY...'); $this->db->query('AND YET ANOTHER QUERY...'); if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); } else { $this->db->trans_commit(); } |
| 94 | |
| 95 | .. note:: Make sure to use $this->db->trans_begin() when running manual |
| 96 | transactions, **NOT** $this->db->trans_start(). |