Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Server Transactions / Package Database Config #543

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ public function connection($name = null)
*/
protected function makeConnection($name)
{
$config = $this->getConfig($name);
// Resolve the package name if there is one.
$parts = explode(':',$name);
$packageName = $parts[0];

if (isset($this->extensions[$name]))
// If the package has a registered connection resolver,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add a new, empty line above here so there is a space between the code above it and the comment.

// then use it to return a connection by name
if (isset($this->extensions[$packageName]))
{
return call_user_func($this->extensions[$name], $config);
$resolver = call_user_func($this->extensions[$packageName], $this->app, $this->factory);
return $resolver->connection($name);
}
$config = $this->getConfig($name);

return $this->factory->make($config, $name);
}
Expand Down Expand Up @@ -180,4 +186,4 @@ public function __call($method, $parameters)
return call_user_func_array(array($this->connection(), $method), $parameters);
}

}
}
38 changes: 37 additions & 1 deletion src/Illuminate/Database/SqlServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,41 @@ protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new Schema\Grammars\SqlServerGrammar);
}

/**
* Execute a Closure within a transaction.
* For MsSql, transactions must be called explicitly using SQL
* because the PDO driver does not support beginning a transaction
* with dblib as of this writing.
*
* @param Closure $callback
* @return mixed
*/
public function transaction(\Closure $callback)
{
$this->pdo->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);

$this->pdo->exec("COMMIT");
}

// 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->pdo->exec("ROLLBACK TRAN");

throw $e;
}

return $result;
}

}
}