Issue #1885542 by larowlan, tim.plunkett: Fixed DatabaseControllerNG does not rollback failed ::save() operations.

8.0.x
webchick 2013-01-11 00:04:34 -08:00
parent dd8e4f2d44
commit 9e4a19df1c
4 changed files with 39 additions and 5 deletions

View File

@ -9,7 +9,6 @@ namespace Drupal\Core\Entity;
use PDO; use PDO;
use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\Query\QueryInterface;
use Exception;
use Drupal\Component\Uuid\Uuid; use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\NestedArray;
@ -492,7 +491,7 @@ class DatabaseStorageController implements EntityStorageControllerInterface {
// Ignore slave server temporarily. // Ignore slave server temporarily.
db_ignore_slave(); db_ignore_slave();
} }
catch (Exception $e) { catch (\Exception $e) {
$transaction->rollback(); $transaction->rollback();
watchdog_exception($this->entityType, $e); watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage, $e->getCode, $e); throw new EntityStorageException($e->getMessage, $e->getCode, $e);
@ -548,7 +547,7 @@ class DatabaseStorageController implements EntityStorageControllerInterface {
return $return; return $return;
} }
catch (Exception $e) { catch (\Exception $e) {
$transaction->rollback(); $transaction->rollback();
watchdog_exception($this->entityType, $e); watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage(), $e->getCode(), $e); throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);

View File

@ -217,7 +217,7 @@ class DatabaseStorageControllerNG extends DatabaseStorageController {
return $return; return $return;
} }
catch (Exception $e) { catch (\Exception $e) {
$transaction->rollback(); $transaction->rollback();
watchdog_exception($this->entityType, $e); watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage(), $e->getCode(), $e); throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);

View File

@ -8,6 +8,7 @@
namespace Drupal\system\Tests\Entity; namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\Core\Database\Database;
/** /**
* Tests invocation of hooks when performing an action. * Tests invocation of hooks when performing an action.
@ -28,7 +29,7 @@ class EntityCrudHookTest extends WebTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('entity_crud_hook_test', 'taxonomy', 'comment', 'file'); public static $modules = array('entity_crud_hook_test', 'taxonomy', 'comment', 'file', 'entity_test');
protected $ids = array(); protected $ids = array();
@ -409,4 +410,29 @@ class EntityCrudHookTest extends WebTestBase {
'entity_crud_hook_test_entity_delete called for type user', 'entity_crud_hook_test_entity_delete called for type user',
)); ));
} }
/**
* Tests rollback from failed insert in EntityNG.
*/
function testEntityNGRollback() {
// Create a block.
try {
$entity = entity_create('entity_test', array('name' => 'fail_insert'))->save();
$this->fail('Expected exception has not been thrown.');
}
catch (\Exception $e) {
$this->pass('Expected exception has been thrown.');
}
if (Database::getConnection()->supportsTransactions()) {
// Check that the block does not exist in the database.
$ids = entity_query('entity_test')->condition('name', 'fail_insert')->execute();
$this->assertTrue(empty($ids), 'Transactions supported, and entity not found in database.');
}
else {
// Check that the block exists in the database.
$ids = entity_query('entity_test')->condition('name', 'fail_insert')->execute();
$this->assertFalse(empty($ids), 'Transactions not supported, and entity found in database.');
}
}
} }

View File

@ -169,3 +169,12 @@ function entity_test_form_node_form_alter(&$form, &$form_state, $form_id) {
$langcode = $form_state['controller']->getFormLangcode($form_state); $langcode = $form_state['controller']->getFormLangcode($form_state);
variable_set('entity_form_langcode', $langcode); variable_set('entity_form_langcode', $langcode);
} }
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function entity_test_entity_test_insert($entity) {
if ($entity->name->value == 'fail_insert') {
throw new Exception("Test exception rollback.");
}
}