Skip to content

Commit

Permalink
Fixed sql server
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Aug 23, 2016
1 parent 7d116dc commit f4588f8
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/Illuminate/Database/SqlServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down

0 comments on commit f4588f8

Please sign in to comment.