diff --git a/src/Illuminate/Database/SqlServerConnection.php b/src/Illuminate/Database/SqlServerConnection.php index 2d848a851d67..cdd172ae2ef7 100755 --- a/src/Illuminate/Database/SqlServerConnection.php +++ b/src/Illuminate/Database/SqlServerConnection.php @@ -16,41 +16,44 @@ class SqlServerConnection extends Connection * Execute a Closure within a transaction. * * @param \Closure $callback + * @param int $attempts * @return mixed * * @throws \Exception|\Throwable */ - public function transaction(Closure $callback) + public function transaction(Closure $callback, $attempts = 1) { - if ($this->getDriverName() == 'sqlsrv') { - return parent::transaction($callback); - } + for ($a = 1; $a <= $attempts; $a++) { + if ($this->getDriverName() == 'sqlsrv') { + return parent::transaction($callback); + } - $this->getPdo()->exec('BEGIN TRAN'); + $this->getPdo()->exec('BEGIN TRAN'); - // We'll simply execute the given callback within a try / catch block - // and if we catch any exception we can rollback the transaction - // so that none of the changes are persisted to the database. - try { - $result = $callback($this); + // We'll simply execute the given callback within a try / catch block + // and if we catch any exception we can rollback the transaction + // so that none of the changes are persisted to the database. + try { + $result = $callback($this); - $this->getPdo()->exec('COMMIT TRAN'); - } + $this->getPdo()->exec('COMMIT TRAN'); + } - // If we catch an exception, we will roll back so nothing gets messed - // up in the database. Then we'll re-throw the exception so it can - // be handled how the developer sees fit for their applications. - catch (Exception $e) { - $this->getPdo()->exec('ROLLBACK TRAN'); + // If we catch an exception, we will roll back so nothing gets messed + // up in the database. Then we'll re-throw the exception so it can + // be handled how the developer sees fit for their applications. + catch (Exception $e) { + $this->getPdo()->exec('ROLLBACK TRAN'); - throw $e; - } catch (Throwable $e) { - $this->getPdo()->exec('ROLLBACK TRAN'); + throw $e; + } catch (Throwable $e) { + $this->getPdo()->exec('ROLLBACK TRAN'); - throw $e; - } + throw $e; + } - return $result; + return $result; + } } /**