Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO transaction issues

I have a simple try catch which is not operating how I would expect. This is my first try at using transactions with PDO:

try
        {
            $dbo = Db::init();
            $dbo->beginTransaction();
            $dbo->exec("TRUNCATE TABLE {$this->table}");
            $dbo->exec($insert);
            $dbo->commit();
        }
        catch(Exception $e)
        {
            $dbo->rollBack();
            echo 'Failed to sync ' . $this->table; 
        }

The problem is, if the $dbo->exec($insert); fails, the $dbo->exec("TRUNCATE TABLE {$this->table}"); does not get rolled back. Any ideas?

like image 324
grep Avatar asked Jun 23 '11 02:06

grep


1 Answers

TRUNCATE cannot be rolled back. Use DELETE instead.

like image 180
zerkms Avatar answered Oct 01 '22 15:10

zerkms