blob: 41f27fc768806aabbf7505487ba81308675fa707 [file] [log] [blame]
adminb728d882006-09-23 20:27:20 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../../userguide.css' />
9
10<script type="text/javascript" src="../../scripts/nav.js"></script>
11<script type="text/javascript" src="../../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../../scripts/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
36<td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
37<td id="breadcrumb_right"><a href="../../toc.html">Full Table of Contents</a></td>
38</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44
45<!-- START BREADCRUMB -->
46<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
47<tr>
48<td id="breadcrumb">
49<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
50<a href="../../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
51<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
52Transactions
53</td>
54<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
55</tr>
56</table>
57<!-- END BREADCRUMB -->
58
59
60<br clear="all" />
61
62
63<!-- START CONTENT -->
64<div id="content">
65
66
67<h1>Transactions</h1>
68
admince586d92006-09-23 23:04:43 +000069<p>Code Igniter's database abstraction allows you to use <dfn>transactions</dfn> will databases that support transaction-safe table types. In MySQL, you'll need
70to be running InnoDB or BDB table types rather then the more common MyISAM. Most other databases support transactions natively.</p>
71
72<p>If you are not familiar with
73transactions we recommend you spend some time learning about them for your particular database. The information below assumes you
74have a basic understanding of transactions.
75</p>
76
77<h2>Code Igniter's Approach to Transactions</h2>
78
79<p>Code Igniter utilizes an approach to transactions that is very similar to the popular database class ADODB. We've chosen that approach
80becuase it greatly simplifies the process of running transactions. In most cases all that is required are two lines of code.</p>
81
82<p>Traditionally transactions have required a fair amount of work to implement since they demand that you to keep track of your queries
83and determine whether to <dfn>commit</dfn> or <dfn>rollback</dfn> based on the success or failure of your queries. In contrast,
84we've implemented a smart transaction system that does all this for you automatically (you can also manage your transactions manually if you choose to).</p>
85
86<h2>Running Transactions</h2>
87
88<p>To run your queries using transactions you will use the <kbd>$this->db->trans_start()</kbd> and <kbd>$this->db->trans_complete()</kbd> functions as follows:</p>
89
90<code>
91<kbd>$this->db->trans_start();</kbd><br />
92$this->db->query('AN SQL QUERY...');<br />
93$this->db->query('ANOTHER QUERY...');<br />
94$this->db->query('AND YET ANOTHER QUERY...');<br />
95<kbd>$this->db->trans_complete();</kbd>
96</code>
97
98<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.</p>
99
100
101<h2>Managing Errors</h2>
102
103<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
104manage your own errors like this:</p>
105
106<code>
107$this->db->trans_start();<br />
108$this->db->query('AN SQL QUERY...');<br />
109$this->db->trans_complete();<br />
110<br />
111if (<kbd>$this->db->trans_status()</kbd> === FALSE)<br />
112{<br />
113&nbsp;&nbsp;&nbsp;&nbsp;// generate an error....<br />
114}
115</code>
116
117
118<h2>Enabling Transactions</h2>
119
120<p>Transactions are enabled automatically the moment you use <kbd>$this->db->trans_start()</kbd>. If you would like to disable transactions you
121can do so using <kbd>$this->db->trans_off()</kbd>:
122
123<code>
124<kbd>$this->db->trans_off()</kbd><br /><br />
125
126$this->db->trans_start();<br />
127$this->db->query('AN SQL QUERY...');<br />
128$this->db->trans_complete();
129</code>
130
131<p>When transactions are disabled, any queries will be auto-commited, just as they are when running queries normally.</p>
132
133
134<h2>Test Mode</h2>
135
136<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.
137To use test mode simply set the first parameter in the <kbd>$this->db->trans_start()</kbd> function to <samp>TRUE</samp>:
138
139<code>
140$this->db->trans_start(</kbd><samp>TRUE</samp><kbd>);<br />
141$this->db->query('AN SQL QUERY...');<br />
142$this->db->trans_complete();
143</code>
144
145
146<h2>Running Transactions Manually</h2>
147
148<p>If you would like to run transactions manually you can do so as follows:</p>
149
150<code>
151$this->db->trans_begin();<br /><br />
152
153$this->db->query('AN SQL QUERY...');<br />
154$this->db->query('ANOTHER QUERY...');<br />
155$this->db->query('AND YET ANOTHER QUERY...');<br />
156
157<br />
158
159if ($this->db->trans_status() === FALSE)<br />
160{<br />
161&nbsp;&nbsp;&nbsp;&nbsp;$this->db->trans_rollback();<br />
162}<br />
163else<br />
164{<br />
165&nbsp;&nbsp;&nbsp;&nbsp;$this->db->trans_commit();<br />
166}<br />
167</code>
168
169<p class="important"><strong>Note:</strong> Make sure to use <kbd>$this->db->trans_begin()</kbd> when running manual trasactions, <strong>NOT</strong>
170<dfn>$this->db->trans_start()</dfn>.</p>
adminb728d882006-09-23 20:27:20 +0000171
172
173
174
175
176
177
178
179</div>
180<!-- END CONTENT -->
181
182
183<div id="footer">
184<p>
185Previous Topic:&nbsp;&nbsp;<a href="index.html">Database Class</a>
186&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
187<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
188<a href="../../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
189Next Topic:&nbsp;&nbsp;<a href="configuration.html">Database Configuration</a>
190<p>
191<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
192</div>
193
194</body>
195</html>