1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Transactions : CodeIgniter User Guide</title>
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
11 <script type="text/javascript" src="../nav/nav.js"></script>
12 <script type="text/javascript" src="../nav/prototype.lite.js"></script>
13 <script type="text/javascript" src="../nav/moo.fx.js"></script>
14 <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
16 <meta http-equiv='expires' content='-1' />
17 <meta http-equiv= 'pragma' content='no-cache' />
18 <meta name='robots' content='all' />
19 <meta name='author' content='ExpressionEngine Dev Team' />
20 <meta name='description' content='CodeIgniter User Guide' />
25 <!-- START NAVIGATION -->
26 <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
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>
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
36 <!-- END NAVIGATION -->
40 <!-- START BREADCRUMB -->
41 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
44 <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
45 <a href="../index.html">User Guide Home</a> ›
46 <a href="index.html">Database Library</a> ›
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>
52 <!-- END BREADCRUMB -->
58 <!-- START CONTENT -->
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
65 to be running InnoDB or BDB table types rather than the more common MyISAM. Most other database platforms support transactions natively.</p>
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
69 have a basic understanding of transactions.
72 <h2>CodeIgniter's Approach to Transactions</h2>
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
75 because it greatly simplifies the process of running transactions. In most cases all that is required are two lines of code.</p>
77 <p>Traditionally, transactions have required a fair amount of work to implement since they demand that you to keep track of your queries
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
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>
83 <h2>Running Transactions</h2>
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>
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>
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>
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>
105 <p>Strict Mode can be disabled as follows:</p>
107 <code>$this->db->trans_strict(FALSE);</code>
110 <h2>Managing Errors</h2>
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>
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 />
121 if (<kbd>$this->db->trans_status()</kbd> === FALSE)<br />
123 // generate an error... or use the log_message() function to log your error<br />
128 <h2>Enabling Transactions</h2>
130 <p>Transactions are enabled automatically the moment you use <dfn>$this->db->trans_start()</dfn>. If you would like to disable transactions you
131 can do so using <dfn>$this->db->trans_off()</dfn>:</p>
134 <kbd>$this->db->trans_off()</kbd><br /><br />
136 $this->db->trans_start();<br />
137 $this->db->query('AN SQL QUERY...');<br />
138 $this->db->trans_complete();
141 <p class="important">When transactions are disabled, your queries will be auto-commited, just as they are when running queries without transactions.</p>
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.
147 To use test mode simply set the first parameter in the <dfn>$this->db->trans_start()</dfn> function to <samp>TRUE</samp>:</p>
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();
156 <h2>Running Transactions Manually</h2>
158 <p>If you would like to run transactions manually you can do so as follows:</p>
161 $this->db->trans_begin();<br /><br />
163 $this->db->query('AN SQL QUERY...');<br />
164 $this->db->query('ANOTHER QUERY...');<br />
165 $this->db->query('AND YET ANOTHER QUERY...');<br />
169 if ($this->db->trans_status() === FALSE)<br />
171 $this->db->trans_rollback();<br />
175 $this->db->trans_commit();<br />
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>
191 Previous Topic: <a href="fields.html">Field MetaData</a> ·
192 <a href="#top">Top of Page</a> ·
193 <a href="../index.html">User Guide Home</a> ·
194 Next Topic: <a href="table_data.html">Table Metadata</a>
196 <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p>