/**
  * Tests \Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber::checkReplicaServer().
  */
 function testSystemInitIgnoresSecondaries()
 {
     // Clone the master credentials to a replica connection.
     // Note this will result in two independent connection objects that happen
     // to point to the same place.
     $connection_info = Database::getConnectionInfo('default');
     Database::addConnectionInfo('default', 'replica', $connection_info['default']);
     db_ignore_replica();
     $kernel = new DrupalKernel('testing', drupal_classloader(), FALSE);
     $event = new GetResponseEvent($kernel, Request::create('http://example.com'), HttpKernelInterface::MASTER_REQUEST);
     $subscriber = new ReplicaDatabaseIgnoreSubscriber();
     $subscriber->checkReplicaServer($event);
     $db1 = Database::getConnection('default', 'default');
     $db2 = Database::getConnection('replica', 'default');
     $this->assertIdentical($db1, $db2, 'System Init ignores secondaries when requested.');
 }
 /**
  * Perform menu-specific rebuilding.
  */
 protected function menuLinksRebuild()
 {
     if ($this->lock->acquire(__FUNCTION__)) {
         $transaction = db_transaction();
         try {
             // Ensure the menu links are up to date.
             $this->menuLinkManager->rebuild();
             // Ignore any database replicas temporarily.
             db_ignore_replica();
         } catch (\Exception $e) {
             $transaction->rollback();
             watchdog_exception('menu', $e);
         }
         $this->lock->release(__FUNCTION__);
     } else {
         // Wait for another request that is already doing this work.
         // We choose to block here since otherwise the router item may not
         // be available during routing resulting in a 404.
         $this->lock->wait(__FUNCTION__);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function save(EntityInterface $entity)
 {
     $transaction = $this->database->startTransaction();
     try {
         $return = parent::save($entity);
         // Ignore replica server temporarily.
         db_ignore_replica();
         return $return;
     } catch (\Exception $e) {
         $transaction->rollback();
         watchdog_exception($this->entityTypeId, $e);
         throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function save(EntityInterface $entity)
 {
     $transaction = $this->database->startTransaction();
     try {
         // Sync the changes made in the fields array to the internal values array.
         $entity->updateOriginalValues();
         $return = parent::save($entity);
         // Ignore replica server temporarily.
         db_ignore_replica();
         return $return;
     } catch (\Exception $e) {
         $transaction->rollback();
         watchdog_exception($this->entityTypeId, $e);
         throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
     }
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function save(EntityInterface $entity)
 {
     // We return SAVED_UPDATED by default because the logic below might not
     // update the entity if its values haven't changed, so returning FALSE
     // would be confusing in that situation.
     $return = SAVED_UPDATED;
     $transaction = $this->database->startTransaction();
     try {
         // Load the stored entity, if any.
         if (!$entity->isNew() && !isset($entity->original)) {
             $id = $entity->id();
             if ($entity->getOriginalId() !== NULL) {
                 $id = $entity->getOriginalId();
             }
             $entity->original = $this->loadUnchanged($id);
         }
         if ($entity->isNew()) {
             $entity->mlid = $this->database->insert($this->entityType->getBaseTable())->fields(array('menu_name' => $entity->menu_name))->execute();
             $entity->enforceIsNew();
         }
         // Unlike the save() method from EntityDatabaseStorage, we invoke the
         // 'presave' hook first because we want to allow modules to alter the
         // entity before all the logic from our preSave() method.
         $this->invokeHook('presave', $entity);
         $entity->preSave($this);
         // If every value in $entity->original is the same in the $entity, there
         // is no reason to run the update queries or clear the caches. We use
         // array_intersect_key() with the $entity as the first parameter because
         // $entity may have additional keys left over from building a router entry.
         // The intersect removes the extra keys, allowing a meaningful comparison.
         if ($entity->isNew() || array_intersect_key(get_object_vars($entity), get_object_vars($entity->original)) != get_object_vars($entity->original)) {
             $return = drupal_write_record($this->entityType->getBaseTable(), $entity, $this->idKey);
             if ($return) {
                 if (!$entity->isNew()) {
                     $this->resetCache(array($entity->{$this->idKey}));
                     $entity->postSave($this, TRUE);
                     $this->invokeHook('update', $entity);
                 } else {
                     $return = SAVED_NEW;
                     $this->resetCache();
                     $entity->enforceIsNew(FALSE);
                     $entity->postSave($this, FALSE);
                     $this->invokeHook('insert', $entity);
                 }
             }
         }
         // Ignore replica server temporarily.
         db_ignore_replica();
         unset($entity->original);
         return $return;
     } catch (\Exception $e) {
         $transaction->rollback();
         watchdog_exception($this->entityTypeId, $e);
         throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
     }
 }