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

[5.4] Force reconnect #15867

Closed
wants to merge 1 commit 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
19 changes: 13 additions & 6 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,24 +791,28 @@ protected function tryAgainIfCausedByLostConnection(QueryException $e, $query, $
/**
* Disconnect from the underlying PDO connection.
*
* @param bool $force
*
* @return void
*/
public function disconnect()
public function disconnect($force = false)
{
$this->setPdo(null)->setReadPdo(null);
$this->setPdo(null, $force)->setReadPdo(null);
}

/**
* Reconnect to the database.
*
* @param bool $force
*
* @return void
*
* @throws \LogicException
*/
public function reconnect()
public function reconnect($force = false)
{
if (is_callable($this->reconnector)) {
return call_user_func($this->reconnector, $this);
return call_user_func($this->reconnector, $this, $force);
}

throw new LogicException('Lost connection and no reconnector available.');
Expand Down Expand Up @@ -981,16 +985,19 @@ public function getReadPdo()
* Set the PDO connection.
*
* @param \PDO|null $pdo
* @param bool $force
* @return $this
*
* @throws \RuntimeException
*/
public function setPdo($pdo)
public function setPdo($pdo, $force = false)
{
if ($this->transactions >= 1) {
if ($this->transactions >= 1 && ! $force) {
throw new RuntimeException("Can't swap PDO instance while within transaction.");
}

$this->transactions = 0;

$this->pdo = $pdo;

return $this;
Expand Down
14 changes: 8 additions & 6 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,26 @@ public function purge($name = null)
* Disconnect from the given database.
*
* @param string $name
* @param bool $force
* @return void
*/
public function disconnect($name = null)
public function disconnect($name = null, $force = true)
{
if (isset($this->connections[$name = $name ?: $this->getDefaultConnection()])) {
$this->connections[$name]->disconnect();
$this->connections[$name]->disconnect($force);
}
}

/**
* Reconnect to the given database.
*
* @param string $name
* @param bool $force
* @return \Illuminate\Database\Connection
*/
public function reconnect($name = null)
public function reconnect($name = null, $force = false)
{
$this->disconnect($name = $name ?: $this->getDefaultConnection());
$this->disconnect($name = $name ?: $this->getDefaultConnection(), $force);

if (! isset($this->connections[$name])) {
return $this->connection($name);
Expand Down Expand Up @@ -193,8 +195,8 @@ protected function prepare(Connection $connection)
// Here we'll set a reconnector callback. This reconnector can be any callable
// so we will set a Closure to reconnect from this manager with the name of
// the connection, which will allow us to reconnect from the connections.
$connection->setReconnector(function ($connection) {
$this->reconnect($connection->getName());
$connection->setReconnector(function ($connection, $force = false) {
$this->reconnect($connection->getName(), $force);
});

return $connection;
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ public function testCantSwapPDOWithOpenTransaction()
$connection->disconnect();
}

public function testCanForceSwapPDOWithOpenTransaction()
{
$pdo = $this->createMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction')->will($this->returnValue(true));
$connection = $this->getMockConnection([], $pdo);
$connection->beginTransaction();
$connection->disconnect(true);
}

public function testBeganTransactionFiresEventsIfSet()
{
$pdo = $this->createMock('DatabaseConnectionTestMockPDO');
Expand Down