CodeIgniter 4: transaction helpers versus manual commit and rollback
Choose between CodeIgniter 4 transaction helpers and manual commit or rollback for service-layer writes.
29 Jan 2026, 19:20 UTC

Decide how to wrap CodeIgniter 4 writes in a transaction
You need to insert or update several rows so that either every change is applied or none of them are. CodeIgniter 4 offers two supported paths: the transaction helper methods transStart() and transComplete() on the database Connection, or manual beginTransaction(), commit(), and rollback() calls. Use the helpers for typical service-layer code; use manual calls when you need explicit exception handling or are integrating code outside CodeIgniter’s query builder.
Constraints to check first
- CodeIgniter 4.x with a database group configured in app/Config/Database.php.
- The underlying storage engine must support transactions. For MySQL, that means InnoDB; MyISAM tables ignore transaction commands.
- Transactions apply to one database connection only. Writes sent through a different connection are not part of the same unit of work.
- Do not hold a transaction across HTTP requests or long-running jobs; open connections and row locks can exhaust the database.
Supported options compared
| Option | How it works | Failure detection | Best for |
|---|---|---|---|
| transStart() / transComplete() | CI4 wrapper around the driver’s transaction methods. | When transStrict is true, transComplete() throws on failure. When false, check transStatus() after transComplete(). | Typical controllers and service classes. |
| beginTransaction() / commit() / rollback() | Direct calls on the Connection object, usually inside try/catch. | Catch exceptions around commit() and call rollback() in the catch block. | Custom error handling or mixed CI4 and non-CI4 database code. |
Trade-offs
The helpers reduce boilerplate, but a failure can go unnoticed if transStrict is disabled and nobody inspects transStatus(). Manual calls make the rollback explicit, but it is easy to forget rollback() in a catch block or to leave a transaction open after an exception. Neither approach creates savepoints: calling transStart() inside an active transaction does not give you an independent inner transaction you can roll back separately.
Verify the configuration
Open app/Config/Database.php and inspect the connection array you use. Look for the transStrict key. CodeIgniter 4 defaults to false when the key is absent, which means transComplete() will not throw and you must check transStatus() yourself.
Run this in the project root to confirm the current setting:
grep -n "transStrict" app/Config/Database.php
Where to run: project root. Permissions: read access to app/Config/Database.php. Expected check: a line such as 'transStrict' => true, or no match, which means the default false applies. Risk: editing the wrong environment group, for example changing 'default' while your app uses 'tests' or 'production'.
Implementation: helper-based transaction in a service
This example assumes $this->db is an active CodeIgniter 4 Connection instance, typically injected through the controller or service constructor.
<?php
namespace App\Services;
use CodeIgniter\Database\Connection;
use RuntimeException;
class OrderService
{
protected $db;
public function __construct(Connection $db)
{
$this->db = $db;
}
public function placeOrder(array $items): int
{
$this->db->transStart();
$this->db->table('orders')->insert([
'customer_id' => 123,
'created_at' => date('Y-m-d H:i:s'),
]);
$orderId = (int) $this->db->insertID();
foreach ($items as $item) {
$this->db->table('order_items')->insert([
'order_id' => $orderId,
'product_id' => $item['product_id'],
'quantity' => (int) $item['quantity'],
]);
}
$this->db->transComplete();
if ($this->db->transStatus() === false) {
throw new RuntimeException('Order transaction was rolled back');
}
return $orderId;
}
}
Expected check: after transComplete(), transStatus() returns true when the writes were committed and false when they were rolled back. With transStrict enabled, transComplete() throws before you reach the status check. Risk: if transStrict is false and you omit the transStatus() check, callers may believe the order was created when it was not.
Implementation: manual commit and rollback
<?php
public function placeOrderManual(array $items): int
{
$this->db->beginTransaction();
try {
$this->db->table('orders')->insert([
'customer_id' => 123,
'created_at' => date('Y-m-d H:i:s'),
]);
$orderId = (int) $this->db->insertID();
foreach ($items as $item) {
$this->db->table('order_items')->insert([
'order_id' => $orderId,
'product_id' => $item['product_id'],
'quantity' => (int) $item['quantity'],
]);
}
$this->db->commit();
return $orderId;
} catch (\Throwable $e) {
$this->db->rollback();
throw $e;
}
}
Where to run: inside a CodeIgniter 4 service or controller, on a local development database first. Permissions: the database user needs INSERT and UPDATE privileges on the target tables. Expected check: commit() succeeds and the rows are visible; on failure, rollback() runs and no partial rows remain. Risk: forgetting rollback() in the catch block leaves the transaction open until the connection is released.
Validate the result
- Use a local database, not production.
- Set transStrict to false temporarily so you can observe transStatus() directly.
- Call the service with data designed to fail, such as inserting a row that violates a NOT NULL constraint or a foreign key.
- After transComplete() or commit(), assert that transStatus() is false and query the table to confirm no row was inserted.
- Restore the original transStrict value before deploying.
Limitations
- CodeIgniter 4’s helpers do not expose savepoints. Do not nest transStart() calls expecting independent rollback points.
- Only writes on the same Connection instance participate. Separate database groups or connections are independent.
- Non-transactional table types, such as MyISAM, accept the commands without providing rollback protection.
- Long-running transactions hold database connections and can cause lock contention under concurrent traffic.
Quick decision
- Use transStart() / transComplete() with transStatus() checks for standard CI4 service-layer writes.
- Enable transStrict in app/Config/Database.php if you want failures to throw automatically.
- Use beginTransaction() / commit() / rollback() when you need fine-grained exception handling around the commit itself.