Skip to content

Commit c815e7f

Browse files
authored
Merge pull request #5949 from michaelrk02/develop
[docs] add DB nested transaction
2 parents 16241f0 + 0bdeb91 commit c815e7f

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

user_guide_src/source/database/transactions.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,20 @@ If you would like to run transactions manually you can do so as follows:
9797

9898
.. note:: Make sure to use ``$this->db->transBegin()`` when running manual
9999
transactions, **NOT** ``$this->db->transStart()``.
100+
101+
Nested Transactions
102+
===================
103+
104+
In CodeIgniter, transactions can be nested in a way such that only the
105+
outmost or top-level transaction commands are executed. You can include as
106+
many pairs of ``transStart()``/``transComplete()`` or ``transBegin()``/``transCommit()``/``transRollback()``
107+
as you want inside a transaction block and so on. CodeIgniter will keep
108+
track of the transaction "depth" and only take action at the outermost layer
109+
(zero depth).
110+
111+
.. literalinclude:: transactions/007.php
112+
113+
.. note:: In case the structure is far more complex, it's your responsibility
114+
to ensure that the inner transactions can reach the outermost layer again
115+
in order to be fully executed by the database, thus prevents unintended
116+
commits/rollbacks.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$this->db->transStart(); // actually starts a transaction
4+
$this->db->query('SOME QUERY 1 ...');
5+
$this->db->transStart(); // doesn't necessarily start another transaction
6+
$this->db->query('SOME QUERY 2 ...');
7+
$this->db->transComplete(); // doesn't necessarily end the transaction, but required to finish the inner transaction
8+
$this->db->query('SOME QUERY 3 ...');
9+
$this->db->transComplete(); // actually ends the transaction

0 commit comments

Comments
 (0)