IN FAILED SQL TRANSACTION

Hi there!

Here is my code:


$transaction=Yii::app()->db->beginTransaction();

$code=0;

$success=true;

try

{

   $file->save();

}catch (Exception $e) {

   $code=$e->getCode();

   $e->handled=true;

}

if ($code==0||$code==23505)

{

   $cleanContract=Yii::app()->db->createCommand(

                        "DELETE FROM contract"

                    )->query();

}

else

{

   report_error("Error when saving file $path");

   $success = false;

}

if($success)

   $transaction->commit();

else

{

   $transaction->rollback();

}

return $success;

When I try to execute query cleancontract, or any other query, I get CDbException "In failed sql transaction". Why? How could I fix it?

Note: Failed sql transaction appear only when i have error 23505 during my save.

Thanks!

Have you run your application (or part of it, which causes this problem) with error-logging enabled? Have tried to see, what do you have in your dump after running into this problem?

If not, then consider adding following to your application’s main configuration file:


//Error-logging component

'log'=>array

(

    	'class'=>'CLogRouter',

    	'routes'=>array

    	(

            	array

            	(

                    	'class'=>'CFileLogRoute',

                    	'categories'=>'system.db.*',

            	),

            	array

            	(

                    	'class'=>'CWebLogRoute',

                    	'categories'=>'system.db.*',

            	),

    	),

),

Then look at the bottom of your page (CWebLogRoute) or in protected/runtime folder (CFileLogRoute).

If you still can’t see source of your problems, disable error logging limitations, by removing these lines from above code:


'categories'=>'system.db.*',

But then your logger will try to log everything and it might be even harder do find what seems to be the problem.

First, I have an error on the save function: code 23505, it’s normal, I continue execution.

Then, I have a second error on my cleancontract query:

In fact, I think I must handled my exception attachEventHandler() but I don’t know how to use it in this case.

Without handler, error is detected on the next query, so transaction is stopped and rollback, Am I wrong?