/** * Executes the given command and optionally returns a value * * @param object $command * @param callable $next * @return mixed * @throws Exception */ public function execute($command, callable $next) { $this->entityManager->beginTransaction(); try { $returnValue = $next($command); $this->entityManager->flush(); $this->entityManager->commit(); } catch (Exception $e) { $this->entityManager->close(); $this->entityManager->rollback(); throw $e; } return $returnValue; }
/** * {@inheritdoc} */ public function batchUpdate(MassActionInterface $massAction, IterableResultInterface $results, array $data) { $this->entityName = $massAction->getOptions()->offsetGet('entityName'); $this->fieldName = empty($data['mass_edit_field']) ? null : $data['mass_edit_field']; if (empty($this->fieldName)) { throw new \RuntimeException("Field name was not specified with option 'mass_edit_field'"); } $this->identifierName = $this->doctrineHelper->getSingleEntityIdentifierFieldName($this->entityName); $value = $data[$this->fieldName]; $selectedIds = []; $entitiesCount = 0; $iteration = 0; $this->entityManager = $this->doctrineHelper->getEntityManager($this->entityName); $this->entityManager->beginTransaction(); try { set_time_limit(0); foreach ($results as $result) { /** @var $result ResultRecordInterface */ $selectedIds[] = $result->getValue($this->identifierName); $iteration++; if ($iteration % $this->batchSize == 0) { $entitiesCount += $this->finishBatch($selectedIds, $value); } } if ($iteration % $this->batchSize > 0) { $entitiesCount += $this->finishBatch($selectedIds, $value); } $this->entityManager->commit(); } catch (\Exception $e) { $this->entityManager->rollback(); throw $e; } return $entitiesCount; }
/** * Creates and persists a unique shortcode generated for provided url * * @param UriInterface $url * @param string[] $tags * @return string * @throws InvalidUrlException * @throws RuntimeException */ public function urlToShortCode(UriInterface $url, array $tags = []) { // If the url already exists in the database, just return its short code $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy(['originalUrl' => $url]); if (isset($shortUrl)) { return $shortUrl->getShortCode(); } // Check that the URL exists $this->checkUrlExists($url); // Transactionally insert the short url, then generate the short code and finally update the short code try { $this->em->beginTransaction(); // First, create the short URL with an empty short code $shortUrl = new ShortUrl(); $shortUrl->setOriginalUrl($url); $this->em->persist($shortUrl); $this->em->flush(); // Generate the short code and persist it $shortCode = $this->convertAutoincrementIdToShortCode($shortUrl->getId()); $shortUrl->setShortCode($shortCode)->setTags($this->tagNamesToEntities($this->em, $tags)); $this->em->flush(); $this->em->commit(); return $shortCode; } catch (ORMException $e) { if ($this->em->getConnection()->isTransactionActive()) { $this->em->rollback(); $this->em->close(); } throw new RuntimeException('An error occurred while persisting the short URL', -1, $e); } }
/** * @return void */ public function flush() { try { foreach ($this->data as $data) { $this->prepare($data); $this->entityManager->persist($data); } $this->entityManager->flush(); } catch (ORMException $e) { $this->entityManager->rollback(); } }
/** * {@inheritDoc} */ public function getIterator() { $iteration = 0; $resultSet = clone $this->resultSet; $this->entityManager->beginTransaction(); try { foreach ($resultSet as $key => $value) { $iteration += 1; if (!is_object($value)) { (yield $key => $value); $this->flushAndClearBatch($iteration); continue; } (yield $key => $this->reFetchObject($value)); $this->flushAndClearBatch($iteration); } } catch (\Exception $exception) { $this->entityManager->rollback(); throw $exception; } $this->flushAndClearEntityManager(); $this->entityManager->commit(); }
function it_should_throw_an_exception_if_em_transaction_rollback_failed(EntityManagerInterface $entityManager) { $entityManager->rollback()->willThrow('\\Exception'); $this->shouldThrow('\\RemiSan\\TransactionManager\\Exception\\RollbackException')->duringRollback(); }
/** * {@inheritdoc} */ public function rollback() { return $this->wrapped->rollback(); }
/** * @return bool */ public function rollBack() { $this->em->rollback(); return true; }
/** * {@inheritDoc} */ public function rollback($key = null) { $this->entityManager->rollback(); }
public function execute() { $this->entityManager->getConnection()->setTransactionIsolation(Connection::TRANSACTION_SERIALIZABLE); $this->entityManager->beginTransaction(); try { $payload = $this->getContent(); $ext = false; echo "processing >> " . $payload['image_src'] . " >> for id >> " . $payload['page_id'] . " >> for ext >> " . $payload['image_ext'] . "\n"; try { $this->httpClient->setUri($payload['image_src']); $this->httpClient->getRequest()->setMethod('HEAD'); $response = $this->httpClient->send(); if ($response->getHeaders()->get('Content-Type') !== false) { $ext = $this->get_extension($response->getHeaders()->get('Content-Type')->getFieldValue()); } } catch (\Zend\Http\Exception\InvalidArgumentException $e) { echo "Exception: while sending HEAD method >> " . $e->getMessage() . "\n"; } echo "declared ext >> " . $payload['image_ext'] . " >> detected ext >> " . $ext . "\n"; if ($ext === false) { echo "Not an image \n"; $pageEntity = $this->updatePending($payload['page_id']); echo "processed >> " . $payload['image_src'] . " >> pendingCnt >> " . $pageEntity->getPendingImagesCnt() . " >> status >> " . $pageEntity->getStatus() . "\n"; $this->entityManager->flush(); $this->entityManager->commit(); return; } $this->httpClient->getRequest()->setMethod('GET'); $response = $this->httpClient->send(); if ($ext == 'svg') { $xmlget = simplexml_load_string($response->getBody()); $xmlattributes = $xmlget->attributes(); $width = preg_replace('/[^0-9.]/', '', strtolower((string) $xmlattributes->width)); $height = preg_replace('/[^0-9.]/', '', strtolower((string) $xmlattributes->height)); $imageInfo = [$width, $height, 'mime' => $response->getHeaders()->get('Content-Type')->getFieldValue()]; $imageSize = mb_strlen($response->getBody(), '8bit'); } else { $imageInfo = getimagesizefromstring($response->getBody()); $contentLength = $response->getHeaders()->get('Content-Length'); if ($contentLength === false) { $imageSize = mb_strlen($response->getBody(), '8bit'); } else { $imageSize = $contentLength->getFieldValue(); } } $url = $this->storageService->store($payload['image_ext'], $response->getBody()); $imageEntity = new Images(); $imageEntity->setContentType($imageInfo['mime']); $imageEntity->setWidth($imageInfo[0]); $imageEntity->setHeight($imageInfo[1]); $imageEntity->setSize($imageSize); $imageEntity->setLocalPath($url); $imageEntity->setRemotePath($payload['image_src']); $pageEntity = $this->updatePending($payload['page_id']); $imageEntity->setPage($pageEntity); $this->entityManager->persist($imageEntity); echo "processed >> " . $payload['image_src'] . " >> pendingCnt >> " . $pageEntity->getPendingImagesCnt() . " >> status >> " . $pageEntity->getStatus() . "\n"; $this->entityManager->flush(); $this->entityManager->commit(); } catch (\Exception $e) { $this->entityManager->rollback(); echo "Exception : >>>> " . $e->getMessage() . "\n"; throw new ReleasableException(array('priority' => 10, 'delay' => 15)); } }