Browse Source

Add warning log when a connection with active transaction is going to be closed.

Tadahisa MOTOOKA 8 years ago
parent
commit
1ad87e40e3
2 changed files with 23 additions and 0 deletions
  1. 4 0
      src/Database/Connection.php
  2. 19 0
      tests/TestCase/Database/ConnectionTest.php

+ 4 - 0
src/Database/Connection.php

@@ -25,6 +25,7 @@ use Cake\Database\Log\QueryLogger;
 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.
@@ -127,6 +128,9 @@ class Connection implements ConnectionInterface
      */
     public function __destruct()
     {
+        if ($this->_transactionStarted) {
+            Log::warning('The connection is going to be closed but the transaction is still active.');
+        }
         unset($this->_driver);
     }
 

+ 19 - 0
tests/TestCase/Database/ConnectionTest.php

@@ -488,6 +488,25 @@ class ConnectionTest extends TestCase
     }
 
     /**
+     * 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
      *