Skip to content

Commit

Permalink
Add warning log when a connection with active transaction is going to…
Browse files Browse the repository at this point in the history
… be closed.
  • Loading branch information
motooka committed Aug 12, 2017
1 parent ae74105 commit 1ad87e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Database/Connection.php
Expand Up @@ -25,6 +25,7 @@
use Cake\Database\Schema\CachedCollection;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Datasource\ConnectionInterface;
use Cake\Log\Log;

/**
* Represents a connection with a database server.
Expand Down Expand Up @@ -127,6 +128,9 @@ public function __construct($config)
*/
public function __destruct()
{
if ($this->_transactionStarted) {
Log::warning('The connection is going to be closed but the transaction is still active.');
}
unset($this->_driver);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -487,6 +487,25 @@ public function testSimpleTransactions()
$this->assertCount(1, $result);
}

/**
* Tests that the destructor of Connection does not crash
* when transaction is not closed
*
* @return void
*/
public function testCloseConnectionWithUncommittedTransaction()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMockBuilder('\Cake\Database\Connection')
->setMethods(['connect'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
$connection->begin();
$this->assertTrue($connection->inTransaction());

// a warning log will be generated by __destruct of Connection.
}

/**
* Tests that it is possible to use virtualized nested transaction
* with early rollback algorithm
Expand Down

0 comments on commit 1ad87e4

Please sign in to comment.