﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>PHP博客-博观而约取，厚积而薄发。-文章分类-技术文章</title><link>http://www.phpweblog.net/peiyinjin/category/451.html</link><description>再长的路，一步步也能走完；再短的路，不迈开双脚也无法到达。</description><language>zh-cn</language><lastBuildDate>Tue, 15 Apr 2008 09:11:40 GMT</lastBuildDate><pubDate>Tue, 15 Apr 2008 09:11:40 GMT</pubDate><ttl>60</ttl><item><title>CodeIgniter中的事务详解</title><link>http://www.phpweblog.net/peiyinjin/articles/3200.html</link><dc:creator>Peter Jin</dc:creator><author>Peter Jin</author><pubDate>Tue, 15 Apr 2008 06:35:00 GMT</pubDate><guid>http://www.phpweblog.net/peiyinjin/articles/3200.html</guid><wfw:comment>http://www.phpweblog.net/peiyinjin/comments/3200.html</wfw:comment><comments>http://www.phpweblog.net/peiyinjin/articles/3200.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.phpweblog.net/peiyinjin/comments/commentRss/3200.html</wfw:commentRss><trackback:ping>http://www.phpweblog.net/peiyinjin/services/trackbacks/3200.html</trackback:ping><description><![CDATA[<p>CodeIgniter的数据库抽象允许你在支持事务安全的数据库表中使用<dfn>事务</dfn>在MySQL中，你需要用InnoDB或BDB表而不是更常用的MyISAM。Most other database platforms support transactions natively.</p>
<p>如果你对事务不熟悉，我们建议您上网寻找相关资源学习。以下信息假设你已经明白事务的相关概念。</p>
<h2>CodeIgniter's Approach to Transactions
<hr>
</h2>
<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 because it greatly simplifies the process of running transactions. In most cases all that is required are two lines of code.</p>
<p>Traditionally, transactions have required a fair amount of work to implement since they demand that you to keep track of your queries 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 nested queries. In contrast, 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, but there's really no benefit).</p>
<h2>Running Transactions
<hr>
</h2>
<p>To run your queries using transactions you will use the <dfn>$this-&gt;db-&gt;trans_start()</dfn> and <dfn>$this-&gt;db-&gt;trans_complete()</dfn> functions as follows:</p>
<code><kbd>$this-&gt;db-&gt;trans_start();</kbd><br>$this-&gt;db-&gt;query('AN SQL QUERY...');<br>$this-&gt;db-&gt;query('ANOTHER QUERY...');<br>$this-&gt;db-&gt;query('AND YET ANOTHER QUERY...');<br><kbd>$this-&gt;db-&gt;trans_complete();</kbd> </code>
<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 of any given query.</p>
<h2>Managing Errors
<hr>
</h2>
<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 manage your own errors like this:</p>
<code>$this-&gt;db-&gt;trans_start();<br>$this-&gt;db-&gt;query('AN SQL QUERY...');<br>$this-&gt;db-&gt;query('ANOTHER QUERY...');<br>$this-&gt;db-&gt;trans_complete();<br><br>if (<kbd>$this-&gt;db-&gt;trans_status()</kbd> === FALSE)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;// generate an error... or use the log_message() function to log your error<br>} </code>
<h2>Enabling Transactions
<hr>
</h2>
<p>&nbsp;Transactions are enabled automatically the moment you use <dfn>$this-&gt;db-&gt;trans_start()</dfn>. If you would like to disable transactions you can do so using <dfn>$this-&gt;db-&gt;trans_off()</dfn>:</p>
<code><kbd>$this-&gt;db-&gt;trans_off()</kbd><br><br>$this-&gt;db-&gt;trans_start();<br>$this-&gt;db-&gt;query('AN SQL QUERY...');<br>$this-&gt;db-&gt;trans_complete(); </code>
<p class=important>When transactions are disabled, your queries will be auto-commited, just as they are when running queries without transactions.</p>
<h2>Test Mode
<hr>
</h2>
<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. To use test mode simply set the first parameter in the <dfn>$this-&gt;db-&gt;trans_start()</dfn> function to <samp>TRUE</samp>:</p>
<code>$this-&gt;db-&gt;trans_start(<samp>TRUE</samp>); // Query will be rolled back<br>$this-&gt;db-&gt;query('AN SQL QUERY...');<br>$this-&gt;db-&gt;trans_complete(); </code>
<h2>Running Transactions Manually
<hr>
</h2>
<p>If you would like to run transactions manually you can do so as follows:</p>
<code>$this-&gt;db-&gt;trans_begin();<br><br>$this-&gt;db-&gt;query('AN SQL QUERY...');<br>$this-&gt;db-&gt;query('ANOTHER QUERY...');<br>$this-&gt;db-&gt;query('AND YET ANOTHER QUERY...');<br><br>if ($this-&gt;db-&gt;trans_status() === FALSE)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;db-&gt;trans_rollback();<br>}<br>else<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;db-&gt;trans_commit();<br>}<br>
<hr>
</code><span style="COLOR: red"><strong>Note:</strong></span> <span style="COLOR: red"><strong>Make sure to use <kbd>$this-&gt;db-&gt;trans_begin()</kbd> when running manual transactions, NOT <dfn>$this-&gt;db-&gt;trans_start()</dfn>.<br>
<hr>
</strong></span>
<img src ="http://www.phpweblog.net/peiyinjin/aggbug/3200.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.phpweblog.net/peiyinjin/" target="_blank">Peter Jin</a> 2008-04-15 14:35 <a href="http://www.phpweblog.net/peiyinjin/articles/3200.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>