예제 #1
0
 public function testCreateWithWeirdPdoExceptionCodeDocumentGetCodeReturnValue()
 {
     $code = 'HY000';
     $pdoException = new PDOExceptionStub('Unknow or incorrect', $code);
     $newException = RuntimeException::createFromException($pdoException);
     $this->assertEquals($code, $newException->getCode());
 }
예제 #2
0
 /**
  * @param Config $config
  * @return \PDO
  * @throws UnknownDatabaseException
  * @throws RuntimeException
  */
 public function __invoke(Config $config)
 {
     $attempt = 0;
     $maxAttempts = $config->getMaximumAttempts();
     while (++$attempt <= $maxAttempts) {
         try {
             $connection = $this->create($config);
             $connection->prepare('SET NAMES ?, time_zone = ?')->execute([$config->getCharset(), $config->getTimezone()]);
             return $connection;
         } catch (\PDOException $exception) {
             if (UnknownDatabaseException::isUnknownDatabase($exception)) {
                 throw UnknownDatabaseException::createFromUnknownDatabase($config->getDatabase(), $exception);
             }
             if ($maxAttempts > $attempt) {
                 // more tries left, so we'll log this error
                 //                    $template = 'Failed connection to "%s" on attempt %d with error "%s"';
                 //                    $dsn      = $config->getDsn();
                 //                    error_log(sprintf($template, $dsn, $attempt, $exception->getMessage()));
                 //                    $logger->error(sprintf($template, $dsn, $attempt, $exception->getMessage()), [
                 //                        'e_message' => $exception->getMessage(),
                 //                        'e_code'    => $exception->getCode(),
                 //                        'e_file'    => $exception->getFile(),
                 //                        'e_line'    => $exception->getLine(),
                 //                        'e_trace'   => $exception->getTraceAsString()
                 //                    ]);
                 // sleep with some exponential backoff
                 $msec = pow(2, $attempt) * 50;
                 usleep($msec * 1000);
             } else {
                 // ran out of attempts, throw the last error
                 throw RuntimeException::createFromException($exception);
             }
         }
     }
 }
예제 #3
0
파일: Adapter.php 프로젝트: phlib/db
 /**
  * @param bool $hasCaughtException
  * @return bool
  */
 protected function doBeginTransaction($hasCaughtException = false)
 {
     try {
         return $this->getConnection()->beginTransaction();
     } catch (\PDOException $exception) {
         if (RuntimeException::hasServerGoneAway($exception) && !$hasCaughtException) {
             $this->reconnect();
             return $this->doBeginTransaction(true);
         }
         throw RuntimeException::createFromException($exception);
     }
 }