admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Allard | 8039d4c | 2008-05-31 02:47:56 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
Derek Jones | fd93d22 | 2008-05-06 15:18:50 +0000 | [diff] [blame] | 6 | <title>Transactions : CodeIgniter User Guide</title>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 7 |
|
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 10 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 15 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 21 |
|
| 22 | </head>
|
| 23 | <body>
|
| 24 |
|
| 25 | <!-- START NAVIGATION -->
|
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 28 | <div id="masthead">
|
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 30 | <tr>
|
Rick Ellis | 2594953 | 2008-08-26 19:48:08 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.7</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 33 | </tr>
|
| 34 | </table>
|
| 35 | </div>
|
| 36 | <!-- END NAVIGATION -->
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 | <!-- START BREADCRUMB -->
|
| 41 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 42 | <tr>
|
| 43 | <td id="breadcrumb">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 44 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 45 | <a href="../index.html">User Guide Home</a> ›
|
| 46 | <a href="index.html">Database Library</a> ›
|
| 47 | Transactions
|
| 48 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 49 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 50 | </tr>
|
| 51 | </table>
|
| 52 | <!-- END BREADCRUMB -->
|
| 53 |
|
| 54 |
|
| 55 | <br clear="all" />
|
| 56 |
|
| 57 |
|
| 58 | <!-- START CONTENT -->
|
| 59 | <div id="content">
|
| 60 |
|
| 61 |
|
| 62 | <h1>Transactions</h1>
|
| 63 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 64 | <p>CodeIgniter's database abstraction allows you to use <dfn>transactions</dfn> with databases that support transaction-safe table types. In MySQL, you'll need
|
Derek Allard | cb36e34 | 2008-08-31 14:24:56 +0000 | [diff] [blame] | 65 | to be running InnoDB or BDB table types rather than the more common MyISAM. Most other database platforms support transactions natively.</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 66 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 67 | <p>If you are not familiar with
|
| 68 | transactions we recommend you find a good online resource to learn about them for your particular database. The information below assumes you
|
admin | a33ec0a | 2006-10-10 23:38:05 +0000 | [diff] [blame] | 69 | have a basic understanding of transactions.
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 70 | </p>
|
| 71 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 72 | <h2>CodeIgniter's Approach to Transactions</h2>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 73 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 74 | <p>CodeIgniter utilizes an approach to transactions that is very similar to the process used by the popular database class ADODB. We've chosen that approach
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 75 | because it greatly simplifies the process of running transactions. In most cases all that is required are two lines of code.</p>
|
| 76 |
|
admin | d125c5c | 2006-09-25 23:26:12 +0000 | [diff] [blame] | 77 | <p>Traditionally, transactions have required a fair amount of work to implement since they demand that you to keep track of your queries
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 78 | and determine whether to <dfn>commit</dfn> or <dfn>rollback</dfn> based on the success or failure of your queries. This is particularly cumbersome with
|
admin | d125c5c | 2006-09-25 23:26:12 +0000 | [diff] [blame] | 79 | nested queries. In contrast,
|
| 80 | we've implemented a smart transaction system that does all this for you automatically (you can also manage your transactions manually if you choose to,
|
| 81 | but there's really no benefit).</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 82 |
|
| 83 | <h2>Running Transactions</h2>
|
| 84 |
|
| 85 | <p>To run your queries using transactions you will use the <dfn>$this->db->trans_start()</dfn> and <dfn>$this->db->trans_complete()</dfn> functions as follows:</p>
|
| 86 |
|
| 87 | <code>
|
| 88 | <kbd>$this->db->trans_start();</kbd><br />
|
| 89 | $this->db->query('AN SQL QUERY...');<br />
|
| 90 | $this->db->query('ANOTHER QUERY...');<br />
|
| 91 | $this->db->query('AND YET ANOTHER QUERY...');<br />
|
| 92 | <kbd>$this->db->trans_complete();</kbd>
|
| 93 | </code>
|
| 94 |
|
admin | d125c5c | 2006-09-25 23:26:12 +0000 | [diff] [blame] | 95 | <p>You can run as many queries as you want between the start/complete functions and they will all be committed or rolled back based on success or failure
|
| 96 | of any given query.</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 97 |
|
| 98 |
|
Rick Ellis | 2d2b2b9 | 2008-05-12 18:16:30 +0000 | [diff] [blame] | 99 | <h2>Strict Mode</h2>
|
| 100 |
|
| 101 | <p>By default CodeIgniter runs all transactions in <dfn>Strict Mode</dfn>. When strict mode is enabled, if you are running multiple groups of
|
| 102 | transactions, if one group fails all groups will be rolled back. If strict mode is disabled, each group is treated independently, meaning
|
| 103 | a failure of one group will not affect any others.</p>
|
| 104 |
|
| 105 | <p>Strict Mode can be disabled as follows:</p>
|
| 106 |
|
| 107 | <code>$this->db->trans_strict(FALSE);</code>
|
| 108 |
|
| 109 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 110 | <h2>Managing Errors</h2>
|
| 111 |
|
| 112 | <p>If you have error reporting enabled in your <dfn>config/database.php</dfn> file you'll see a standard error message if the commit was unsuccessful. If debugging is turned off, you can
|
| 113 | manage your own errors like this:</p>
|
| 114 |
|
| 115 | <code>
|
| 116 | $this->db->trans_start();<br />
|
| 117 | $this->db->query('AN SQL QUERY...');<br />
|
| 118 | $this->db->query('ANOTHER QUERY...');<br />
|
| 119 | $this->db->trans_complete();<br />
|
| 120 | <br />
|
| 121 | if (<kbd>$this->db->trans_status()</kbd> === FALSE)<br />
|
| 122 | {<br />
|
admin | d125c5c | 2006-09-25 23:26:12 +0000 | [diff] [blame] | 123 | // generate an error... or use the log_message() function to log your error<br />
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 124 | }
|
| 125 | </code>
|
| 126 |
|
| 127 |
|
| 128 | <h2>Enabling Transactions</h2>
|
| 129 |
|
| 130 | <p>Transactions are enabled automatically the moment you use <dfn>$this->db->trans_start()</dfn>. If you would like to disable transactions you
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 131 | can do so using <dfn>$this->db->trans_off()</dfn>:</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 132 |
|
| 133 | <code>
|
| 134 | <kbd>$this->db->trans_off()</kbd><br /><br />
|
| 135 |
|
| 136 | $this->db->trans_start();<br />
|
| 137 | $this->db->query('AN SQL QUERY...');<br />
|
| 138 | $this->db->trans_complete();
|
| 139 | </code>
|
| 140 |
|
| 141 | <p class="important">When transactions are disabled, your queries will be auto-commited, just as they are when running queries without transactions.</p>
|
| 142 |
|
| 143 |
|
| 144 | <h2>Test Mode</h2>
|
| 145 |
|
| 146 | <p>You can optionally put the transaction system into "test mode", which will cause your queries to be rolled back -- even if the queries produce a valid result.
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 147 | To use test mode simply set the first parameter in the <dfn>$this->db->trans_start()</dfn> function to <samp>TRUE</samp>:</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 148 |
|
| 149 | <code>
|
| 150 | $this->db->trans_start(<samp>TRUE</samp>); // Query will be rolled back<br />
|
| 151 | $this->db->query('AN SQL QUERY...');<br />
|
| 152 | $this->db->trans_complete();
|
| 153 | </code>
|
| 154 |
|
| 155 |
|
| 156 | <h2>Running Transactions Manually</h2>
|
| 157 |
|
| 158 | <p>If you would like to run transactions manually you can do so as follows:</p>
|
| 159 |
|
| 160 | <code>
|
| 161 | $this->db->trans_begin();<br /><br />
|
| 162 |
|
| 163 | $this->db->query('AN SQL QUERY...');<br />
|
| 164 | $this->db->query('ANOTHER QUERY...');<br />
|
| 165 | $this->db->query('AND YET ANOTHER QUERY...');<br />
|
| 166 |
|
| 167 | <br />
|
| 168 |
|
| 169 | if ($this->db->trans_status() === FALSE)<br />
|
| 170 | {<br />
|
| 171 | $this->db->trans_rollback();<br />
|
| 172 | }<br />
|
| 173 | else<br />
|
| 174 | {<br />
|
| 175 | $this->db->trans_commit();<br />
|
| 176 | }<br />
|
| 177 | </code>
|
| 178 |
|
| 179 | <p class="important"><strong>Note:</strong> Make sure to use <kbd>$this->db->trans_begin()</kbd> when running manual transactions, <strong>NOT</strong>
|
| 180 | <dfn>$this->db->trans_start()</dfn>.</p>
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 185 | </div>
|
| 186 | <!-- END CONTENT -->
|
| 187 |
|
| 188 |
|
| 189 | <div id="footer">
|
| 190 | <p>
|
Derek Allard | 9da4dbc | 2007-04-03 11:39:35 +0000 | [diff] [blame] | 191 | Previous Topic: <a href="fields.html">Field MetaData</a> ·
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 192 | <a href="#top">Top of Page</a> ·
|
| 193 | <a href="../index.html">User Guide Home</a> ·
|
Derek Allard | 9da4dbc | 2007-04-03 11:39:35 +0000 | [diff] [blame] | 194 | Next Topic: <a href="table_data.html">Table Metadata</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 195 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 196 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 197 | </div>
|
| 198 |
|
| 199 | </body>
|
| 200 | </html> |