mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 04:37:42 +08:00
Add transaction/retry support for yii\db\Command
This commit is contained in:
@ -10,6 +10,7 @@ namespace yiiunit\framework\db;
|
||||
use yii\caching\FileCache;
|
||||
use yii\db\Connection;
|
||||
use yii\db\DataReader;
|
||||
use yii\db\Exception;
|
||||
use yii\db\Expression;
|
||||
use yii\db\Schema;
|
||||
|
||||
@ -1129,4 +1130,50 @@ SQL;
|
||||
$db->createCommand()->dropTable($tableName)->execute();
|
||||
$this->assertNull($db->getSchema()->getTableSchema($tableName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group iss
|
||||
*/
|
||||
public function testTransaction()
|
||||
{
|
||||
$connection = $this->getConnection(false);
|
||||
$this->assertNull($connection->transaction);
|
||||
$command = $connection->createCommand("INSERT INTO {{profile}}([[description]]) VALUES('command transaction')");
|
||||
$this->invokeMethod($command, 'requireTransaction');
|
||||
$command->execute();
|
||||
$this->assertNull($connection->transaction);
|
||||
$this->assertEquals(1, $connection->createCommand("SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command transaction'")->queryScalar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group iss
|
||||
*/
|
||||
public function testRetryHandler()
|
||||
{
|
||||
$connection = $this->getConnection(false);
|
||||
$this->assertNull($connection->transaction);
|
||||
$connection->createCommand("INSERT INTO {{profile}}([[description]]) VALUES('command retry')")->execute();
|
||||
$this->assertNull($connection->transaction);
|
||||
$this->assertEquals(1, $connection->createCommand("SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command retry'")->queryScalar());
|
||||
|
||||
$attempts = null;
|
||||
$hitHandler = false;
|
||||
$hitCatch = false;
|
||||
$command = $connection->createCommand("INSERT INTO {{profile}}([[id]], [[description]]) VALUES(1, 'command retry')");
|
||||
$this->invokeMethod($command, 'setRetryHandler', [function ($exception, $attempt) use (&$attempts, &$hitHandler) {
|
||||
$attempts = $attempt;
|
||||
$hitHandler = true;
|
||||
return $attempt <= 2;
|
||||
}]);
|
||||
try {
|
||||
$command->execute();
|
||||
} catch (Exception $e) {
|
||||
$hitCatch = true;
|
||||
$this->assertInstanceOf('yii\db\IntegrityException', $e);
|
||||
}
|
||||
$this->assertNull($connection->transaction);
|
||||
$this->assertSame(3, $attempts);
|
||||
$this->assertTrue($hitHandler);
|
||||
$this->assertTrue($hitCatch);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user