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 |
Rafael Schwemmer | b4b9bd3 | 2015-02-05 15:30:13 +0100 | [diff] [blame] | 24 | implement since they demand that you keep track of your queries and |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 38 | $this->db->trans_start(); |
| 39 | $this->db->query('AN SQL QUERY...'); |
| 40 | $this->db->query('ANOTHER QUERY...'); |
| 41 | $this->db->query('AND YET ANOTHER QUERY...'); |
| 42 | $this->db->trans_complete(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
| 44 | You can run as many queries as you want between the start/complete |
| 45 | functions and they will all be committed or rolled back based on success |
| 46 | or failure of any given query. |
| 47 | |
| 48 | Strict Mode |
| 49 | =========== |
| 50 | |
| 51 | By default CodeIgniter runs all transactions in Strict Mode. When strict |
| 52 | mode is enabled, if you are running multiple groups of transactions, if |
| 53 | one group fails all groups will be rolled back. If strict mode is |
| 54 | disabled, each group is treated independently, meaning a failure of one |
| 55 | group will not affect any others. |
| 56 | |
| 57 | Strict Mode can be disabled as follows:: |
| 58 | |
| 59 | $this->db->trans_strict(FALSE); |
| 60 | |
| 61 | Managing Errors |
| 62 | =============== |
| 63 | |
| 64 | If you have error reporting enabled in your config/database.php file |
| 65 | you'll see a standard error message if the commit was unsuccessful. If |
| 66 | debugging is turned off, you can manage your own errors like this:: |
| 67 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 68 | $this->db->trans_start(); |
| 69 | $this->db->query('AN SQL QUERY...'); |
| 70 | $this->db->query('ANOTHER QUERY...'); |
| 71 | $this->db->trans_complete(); |
| 72 | |
| 73 | if ($this->db->trans_status() === FALSE) |
| 74 | { |
| 75 | // generate an error... or use the log_message() function to log your error |
| 76 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
Andrey Andreev | 777bb98 | 2016-10-24 10:11:22 +0300 | [diff] [blame] | 78 | Disabling Transactions |
| 79 | ====================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
Andrey Andreev | 777bb98 | 2016-10-24 10:11:22 +0300 | [diff] [blame] | 81 | If you would like to disable transactions you can do so using |
| 82 | ``$this->db->trans_off()``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 84 | $this->db->trans_off(); |
| 85 | |
| 86 | $this->db->trans_start(); |
| 87 | $this->db->query('AN SQL QUERY...'); |
| 88 | $this->db->trans_complete(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 89 | |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 90 | When transactions are disabled, your queries will be auto-committed, just as |
Andrey Andreev | 777bb98 | 2016-10-24 10:11:22 +0300 | [diff] [blame] | 91 | they are when running queries without transactions, practically ignoring |
| 92 | any calls to ``trans_start()``, ``trans_complete()``, etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
| 94 | Test Mode |
| 95 | ========= |
| 96 | |
| 97 | You can optionally put the transaction system into "test mode", which |
| 98 | will cause your queries to be rolled back -- even if the queries produce |
| 99 | a valid result. To use test mode simply set the first parameter in the |
| 100 | $this->db->trans_start() function to TRUE:: |
| 101 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 102 | $this->db->trans_start(TRUE); // Query will be rolled back |
| 103 | $this->db->query('AN SQL QUERY...'); |
| 104 | $this->db->trans_complete(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
| 106 | Running Transactions Manually |
| 107 | ============================= |
| 108 | |
| 109 | If you would like to run transactions manually you can do so as follows:: |
| 110 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 111 | $this->db->trans_begin(); |
| 112 | |
| 113 | $this->db->query('AN SQL QUERY...'); |
| 114 | $this->db->query('ANOTHER QUERY...'); |
| 115 | $this->db->query('AND YET ANOTHER QUERY...'); |
| 116 | |
| 117 | if ($this->db->trans_status() === FALSE) |
| 118 | { |
| 119 | $this->db->trans_rollback(); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | $this->db->trans_commit(); |
| 124 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
| 126 | .. note:: Make sure to use $this->db->trans_begin() when running manual |
| 127 | transactions, **NOT** $this->db->trans_start(). |