Transaction problem by overriding save method

I am overriding CActiveRecord’s save method with something like the following:


    public function save($runValidation = true, $attributes = null) 

    {

    	$ts = $this->dbConnection->beginTransaction();

    	try {

    		if ($result = parent::save($runValidation, $attributes)) {	

    			// Update other tables

    			...

    			$ts->commit();

    			return true;

    		} else {

    			$ts->rollback();

    			return $result;

    		}

    	} catch (Exception $e) {

    		$ts->rollback();

    		throw $e;

    	}

    }



The problem is rolling back the transaction doesn’t seem to have an effect on the call to parent::save(). In other words, a new record is created even though the rest of the transaction didn’t complete. The rest of the updates made in the transaction do appear to rollback however.

Here is the MySQL query log:


 20 Query     START TRANSACTION

 20 Query     SELECT * FROM AuthAssignment WHERE userid='1'

 20 Query     SELECT * FROM AuthItem WHERE name='voteUp'

 20 Query     SELECT parent FROM AuthItemChild WHERE child='voteUp'

 20 Query     SELECT * FROM AuthItem WHERE name='superUser'

 20 Query     SELECT parent FROM AuthItemChild WHERE child='superUser'

 20 Query     SELECT * FROM AuthItem WHERE name='moderator'

 20 Query     SELECT parent FROM AuthItemChild WHERE child='moderator'

 20 Query     SELECT * FROM AuthItem WHERE name='admin'

 20 Query     select count(*) as count

              from tn_question_vote

              where question_id = '2' and user_hash = '87d552bd1ece13e3515ffcec0a7a806a'

              and created > 1302314458

 20 Query     select

                        (select count(*)

                        from tn_question_vote

                        where user_id = '1'

                        and created > '2011-04-08 19:00:58') +

                        (select count(*)

                        from tn_answer_vote

                        where user_id = '1'

                        and created > '2011-04-08 19:00:58')

                        as count

20 Query     INSERT INTO `tn_question_vote` (`question_id`, `user_id`, `liked`, `ip_address`, `user_hash`, `created`) VALUES ('2', '1', 1, '127.0.0.1', '87d552bd1ece13e3515ffcec0a7a806a', '2011-04-09 19:00:58')

20 Query     rollback

20 Quit

A tn_question_vote record still gets inserted despite the rollback. Maybe this isn’t an issue with Yii, and is just a misunderstanding of MySQL on my part. Still, does anyone know why the insert goes through?

I checked my table schema again. It appears I had a typo for the table engine type:

ENGINE=INNDB;

The table defaulted to Myisam, and I didn’t see an error from MySQL.

Ignore this thread :)