CodeIgniter Forums
Force database transaction to rollback. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Force database transaction to rollback. (/showthread.php?tid=52809)



Force database transaction to rollback. - El Forum - 06-27-2012

[eluser]Fábio[/eluser]
Hi,

How do I force a transaction to rollback, using the automatic method.
( The manual method supports nested transactions in MySQL? As far as I know it does not support. )

Code:
try
{
    // Begin first transaction
    $this->db->trans_start();
    $this->db->query('<anything>');

        // Begin nested transaction
        $this->db->trans_start();
        $this->db->query('<anything>');

        // Some 'assertion' code
        if( true )
            throw new Exception('Force everything to rollback');

        $this->db->trans_complete();

    // Finishing outermost trans
    $this->db->trans_complete();
}
catch(Exception $ex)
{
    // How do I rollback both transactions?
    // $this->db->rollback(); ??
}

Thanks for your attention.


Force database transaction to rollback. - El Forum - 06-27-2012

[eluser]CroNiX[/eluser]
Haven't tried it, but I believe you would just have one trans_start. Any queries run after that would be part of the same transaction, wouldn't they? And then one trans_complete() after both queries. Then if you rollback it should roll both queries?

From the transactions userguide:
Code:
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE)
{
    // generate an error... or use the log_message() function to log your error
}