Transactions

CodeIgniter’s database abstraction allows you to use transactions withdatabases that support transaction-safe table types. In MySQL, you’llneed to be running InnoDB or BDB table types rather than the more commonMyISAM. Most other database platforms support transactions natively.

If you are not familiar with transactions we recommend you find a goodonline resource to learn about them for your particular database. Theinformation below assumes you have a basic understanding oftransactions.

CodeIgniter’s Approach to Transactions

CodeIgniter utilizes an approach to transactions that is very similar tothe process used by the popular database class ADODB. We’ve chosen thatapproach because it greatly simplifies the process of runningtransactions. In most cases, all that is required is two lines of code.

Traditionally, transactions have required a fair amount of work toimplement since they demand that you keep track of your queries anddetermine whether to commit or rollback based on the success or failureof your queries. This is particularly cumbersome with nested queries. Incontrast, we’ve implemented a smart transaction system that does allthis for you automatically (you can also manage your transactionsmanually if you choose to, but there’s really no benefit).

Running Transactions

To run your queries using transactions you will use the$this->db->transStart() and $this->db->transComplete() functions asfollows:

  1. $this->db->transStart();
  2. $this->db->query('AN SQL QUERY...');
  3. $this->db->query('ANOTHER QUERY...');
  4. $this->db->query('AND YET ANOTHER QUERY...');
  5. $this->db->transComplete();

You can run as many queries as you want between the start/completefunctions and they will all be committed or rolled back based on the successor failure of any given query.

Strict Mode

By default, CodeIgniter runs all transactions in Strict Mode. When strictmode is enabled, if you are running multiple groups of transactions, ifone group fails all groups will be rolled back. If strict mode isdisabled, each group is treated independently, meaning a failure of onegroup will not affect any others.

Strict Mode can be disabled as follows:

  1. $this->db->transStrict(false);

Managing Errors

If you have error reporting enabled in your Config/Database.php fileyou’ll see a standard error message if the commit was unsuccessful. Ifdebugging is turned off, you can manage your own errors like this:

  1. $this->db->transStart();
  2. $this->db->query('AN SQL QUERY...');
  3. $this->db->query('ANOTHER QUERY...');
  4. $this->db->transComplete();
  5.  
  6. if ($this->db->transStatus() === FALSE)
  7. {
  8. // generate an error... or use the log_message() function to log your error
  9. }

Disabling Transactions

Transactions are enabled by default. If you would like to disable transactions youcan do so using $this->db->transOff():

  1. $this->db->transOff();
  2.  
  3. $this->db->transStart();
  4. $this->db->query('AN SQL QUERY...');
  5. $this->db->transComplete();

When transactions are disabled, your queries will be auto-committed, justas they are when running queries without transactions.

Test Mode

You can optionally put the transaction system into “test mode”, whichwill cause your queries to be rolled back – even if the queries producea valid result. To use test mode simply set the first parameter in the$this->db->transStart() function to TRUE:

  1. $this->db->transStart(true); // Query will be rolled back
  2. $this->db->query('AN SQL QUERY...');
  3. $this->db->transComplete();

Running Transactions Manually

If you would like to run transactions manually you can do so as follows:

  1. $this->db->transBegin();
  2.  
  3. $this->db->query('AN SQL QUERY...');
  4. $this->db->query('ANOTHER QUERY...');
  5. $this->db->query('AND YET ANOTHER QUERY...');
  6.  
  7. if ($this->db->transStatus() === FALSE)
  8. {
  9. $this->db->transRollback();
  10. }
  11. else
  12. {
  13. $this->db->transCommit();
  14. }

Note

Make sure to use $this->db->transBegin() when running manualtransactions, NOT $this->db->transStart().