/** * @dataProvider exceptionProvider * * @param $exception * @param $code * @param $contains */ public function testExceptions($exception, $code, $contains) { $formed = PdoException::from($exception); $this->assertInstanceOf('\\Packaged\\Dal\\Exceptions\\Connection\\PdoException', $formed); $this->assertEquals($code, $formed->getCode()); if (is_array($contains)) { foreach ($contains as $contain) { $this->assertContains($contain, $formed->getMessage()); } } else { $this->assertContains($contains, $formed->getMessage()); } $this->assertSame($exception, $formed->getPrevious()); }
/** * @param callable $func * @param callable|null $onError * @param int|null $retryCount * * @return mixed * @throws ConnectionException * @throws PdoException * @throws null */ protected function _performWithRetries(callable $func, callable $onError = null, $retryCount = null) { $this->_lastRetryCount = 0; $this->_recycleConnectionIfRequired(); if ($retryCount === null) { $retryCount = (int) $this->_config()->getItem('retries', 2); } /** @var null|PdoException $exception */ $exception = null; $retries = $retryCount; do { try { $this->_lastRetryCount++; return $func(); } catch (\PDOException $sourceException) { if ($onError) { $onError(); } $exception = PdoException::from($sourceException); if ($retries > 0 && $this->_isRecoverableException($exception)) { if ($this->_shouldReconnectAfterException($exception)) { if ($this->_inTransaction) { error_log('PdoConnection error during transaction: ' . '(' . $exception->getCode() . ') ' . $exception->getMessage()); throw $exception; } $this->disconnect()->connect(); } else { if ($this->_shouldDelayAfterException($exception)) { // Sleep for between 0.1 and 3 milliseconds usleep(mt_rand(100, 3000)); } } } else { error_log('PdoConnection Error: (' . $exception->getCode() . ') ' . $exception->getMessage()); throw $exception; } } $retries--; } while ($retries > 0); if ($exception) { throw $exception; } else { throw new PdoException('An unknown error occurred performing a PDO operation. ' . 'The operation failed after ' . $retryCount . ' retries'); } }