blob: cfd6a566dc93038886bc27f730ab250a2483f257 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001############
2Transactions
3############
4
5CodeIgniter's database abstraction allows you to use transactions with
6databases that support transaction-safe table types. In MySQL, you'll
7need to be running InnoDB or BDB table types rather than the more common
8MyISAM. Most other database platforms support transactions natively.
9
10If you are not familiar with transactions we recommend you find a good
11online resource to learn about them for your particular database. The
12information below assumes you have a basic understanding of
13transactions.
14
15CodeIgniter's Approach to Transactions
16======================================
17
18CodeIgniter utilizes an approach to transactions that is very similar to
19the process used by the popular database class ADODB. We've chosen that
20approach because it greatly simplifies the process of running
21transactions. In most cases all that is required are two lines of code.
22
23Traditionally, transactions have required a fair amount of work to
Rafael Schwemmerb4b9bd32015-02-05 15:30:13 +010024implement since they demand that you keep track of your queries and
Derek Jones8ede1a22011-10-05 13:34:52 -050025determine whether to commit or rollback based on the success or failure
26of your queries. This is particularly cumbersome with nested queries. In
27contrast, we've implemented a smart transaction system that does all
28this for you automatically (you can also manage your transactions
29manually if you choose to, but there's really no benefit).
30
31Running Transactions
32====================
33
34To run your queries using transactions you will use the
35$this->db->trans_start() and $this->db->trans_complete() functions as
36follows::
37
Joseph Wensleyf24f4042011-10-06 22:53:29 -040038 $this->db->trans_start();
39 $this->db->query('AN SQL QUERY...');
40 $this->db->query('ANOTHER QUERY...');
41 $this->db->query('AND YET ANOTHER QUERY...');
42 $this->db->trans_complete();
Derek Jones8ede1a22011-10-05 13:34:52 -050043
44You can run as many queries as you want between the start/complete
45functions and they will all be committed or rolled back based on success
46or failure of any given query.
47
48Strict Mode
49===========
50
51By default CodeIgniter runs all transactions in Strict Mode. When strict
52mode is enabled, if you are running multiple groups of transactions, if
53one group fails all groups will be rolled back. If strict mode is
54disabled, each group is treated independently, meaning a failure of one
55group will not affect any others.
56
57Strict Mode can be disabled as follows::
58
59 $this->db->trans_strict(FALSE);
60
61Managing Errors
62===============
63
64If you have error reporting enabled in your config/database.php file
65you'll see a standard error message if the commit was unsuccessful. If
66debugging is turned off, you can manage your own errors like this::
67
Joseph Wensleyf24f4042011-10-06 22:53:29 -040068 $this->db->trans_start();
69 $this->db->query('AN SQL QUERY...');
70 $this->db->query('ANOTHER QUERY...');
71 $this->db->trans_complete();
72
73 if ($this->db->trans_status() === FALSE)
74 {
75 // generate an error... or use the log_message() function to log your error
76 }
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Andrey Andreev777bb982016-10-24 10:11:22 +030078Disabling Transactions
79======================
Derek Jones8ede1a22011-10-05 13:34:52 -050080
Andrey Andreev777bb982016-10-24 10:11:22 +030081If you would like to disable transactions you can do so using
82``$this->db->trans_off()``::
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Joseph Wensleyf24f4042011-10-06 22:53:29 -040084 $this->db->trans_off();
85
86 $this->db->trans_start();
87 $this->db->query('AN SQL QUERY...');
88 $this->db->trans_complete();
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Andrey Andreev71d8f722017-01-17 12:01:00 +020090When transactions are disabled, your queries will be auto-committed, just as
Andrey Andreev777bb982016-10-24 10:11:22 +030091they are when running queries without transactions, practically ignoring
92any calls to ``trans_start()``, ``trans_complete()``, etc.
Derek Jones8ede1a22011-10-05 13:34:52 -050093
94Test Mode
95=========
96
97You can optionally put the transaction system into "test mode", which
98will cause your queries to be rolled back -- even if the queries produce
99a valid result. To use test mode simply set the first parameter in the
100$this->db->trans_start() function to TRUE::
101
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400102 $this->db->trans_start(TRUE); // Query will be rolled back
103 $this->db->query('AN SQL QUERY...');
104 $this->db->trans_complete();
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
106Running Transactions Manually
107=============================
108
109If you would like to run transactions manually you can do so as follows::
110
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400111 $this->db->trans_begin();
112
113 $this->db->query('AN SQL QUERY...');
114 $this->db->query('ANOTHER QUERY...');
115 $this->db->query('AND YET ANOTHER QUERY...');
116
117 if ($this->db->trans_status() === FALSE)
118 {
119 $this->db->trans_rollback();
120 }
121 else
122 {
123 $this->db->trans_commit();
124 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
126.. note:: Make sure to use $this->db->trans_begin() when running manual
127 transactions, **NOT** $this->db->trans_start().